Lua read redis data null judgment example code

Time:2021-10-14

preface

WhyluaScript operationredisdatabase

1. Reduce overhead – reduce the number of requests to the redis server

2. Atomic operation – redis executes Lua script as an atom

3. Reusable – other clients can use Lua scripts that have been executed

4. Increase redis flexibility – Lua script can help redis do more things

Lua script itself is small and fast to start

Therefore, starting with redis 2.6.0, redis has built-in Lua interpreter on the server side. I won’t say more about the following. Let’s start the text of this article:

Recently, when cooperating with the mobile terminal debugging, I was caught debugging a gateway error that only appears after the redis cache is cleared. Then, open the log on the server and locate a similar error:

[error] 7#7: *12030 lua entry thread aborted: runtime error: /data/share/apps/lua/access_check.lua:133: bad argument #1 to ‘decode’ (string expected, got userdata)

The main function of this code is to read the data in redis from Lua in openresty and decode it into JSON:

?
1
2
3
4
5
6
7
8
local access_token = redis_client:read_by_key(token_key)
 if access_token == nil then
  -- do something...
  return false
 end
 
 local obj_token = cjson.decode(access_token)
 -- do something

We know the reason by querying the data: when Lua reads the redis data and the returned result is null, the returned result is not nil, but NGX of UserData typenull

Why is it designed like this?

Because nil has a special meaning in Lua, if a variable is set to nil, it is equivalent to telling that the variable is undefined (does not exist). If the result of redis query is set to nil and the key corresponding to the query exists in redis, it is obviously unreasonable to distinguish between null and undefined queries. Therefore, a value of UserData type must be used to indicate that the query record is empty, but it is not equivalent to an undefined variable (NGX. Null).

Therefore, the code can be modified as follows:

?
1
2
3
4
5
6
7
8
local access_token = redis_client:read_by_key(token_key)
 if access_token == ngx.null or access_token == nil then
  -- do something...
  return false
 end
 
 local obj_token = cjson.decode(access_token)
 -- do something

summary

The above is the whole content of this article. I hope the content of this article has a certain reference value for everyone’s study or work. If you have any questions, you can leave a message. Thank you for your support for developepper.

Recommended Today

The selector returned by ngrx store createselector performs one-step debugging of fetching logic

Test source code: import { Component } from ‘@angular/core’; import { createSelector } from ‘@ngrx/store’; export interface State { counter1: number; counter2: number; } export const selectCounter1 = (state: State) => state.counter1; export const selectCounter2 = (state: State) => state.counter2; export const selectTotal = createSelector( selectCounter1, selectCounter2, (counter1, counter2) => counter1 + counter2 ); // […]