问题1 org.springframework.data.redis.serializer.SerializationException: Could not read JSON: Invalid UTF-8 start byte 0x9d
at [Source: (byte[])"李四"; line: 1, column: 3]; nested exception is com.fasterxml.jackson.core.JsonParseException: Invalid UTF-8 start byte 0x9d
at [Source: (byte[])"李四"; line: 1, column: 3]
这个问题是添加key value 时使用命令行导致,获取的时通过API,不能读取
问题2 redisTemplate leftPushAll(K key, V… values) 或者 leftPushAll(K key, Collection values) 以数组或者collection方式添加 注意:不能是ArrayList
redisTemplate.opsForList().leftPushAll(key3, t1.toArray());
redisTemplate.opsForList().leftPushAll(list); //点击进去走的是 V...values 方法 坑 api里面的leftPushAll(Collection list)用不了
改造为
public void leftPushAll(String key, Collection values) { //传入集合 会自动指定自定义类型
redisTemplate.opsForList().leftPushAll(key, values.toArray[new Object[values.size()]]);
}
或者
public void leftPushAll(String key, V... values) { //传入数组 会自动指定自定义类型
redisTemplate.opsForList().leftPushAll(key, values);
}
问题3. ERR Error running script (call to f_bb65bb83b2f8a61cac728df1b1af12175dd208a7): @user_script:9: @user_script: 9: Lua redis() command arguments must be strings or integers
问题4. @enable_strict_lua:15: user_script:1: Script attempted to access nonexistent global variable 'require'
redis中调用 lua脚本
require在Redis中不可用,库已预加载 第一行 local cjson = require "cjson" 不需要写
问题5. hash tag mset在集群模式设置值时错误 CROSSSLOT Keys in request don’t hash to the same slot
HashTag机制可以影响key被分配到的slot,从而可以使用那些被限制在slot中操作。
HashTag即是用{}包裹key的一个子串,如{user:}1, {user:}2。
解决方案HashTag
HashTag即是用{}包裹key的一个子串,如{user:}1, {user:}2
问题6. Redis遇到的问题 SerializationException:Could not read JSON: Could not resolve type id 'com.cmbchina.entity.RedPacketDetailBean' as a subtype of java.lang.Object: no such class found 解决办法
原因:A服务存储对象到redis中时候会对应有一个全路径类名限定。在通过token进行取对象值并强制转换的时候,如果接收对象的全路径名称和redis中保存的不一样的话就会转换失败报错。
解决办法:
方法一:
获取并转换接收redis中的对象时将接收对象的全路径与redis中保持一致。
RewardEmp rewardEmp = (RewardEmp)redisTemplate.execute(redisScript, new StringRedisSerializer(), getRedisSerializer(), paramList, argList.toArray());
方法二
从源头上解决,将保存对象的方式,换成其他方式 com.alibaba.fastjson.JSONObject
5. lua脚本
抢红包脚本
-- local cjson = require "cjson"
local prefix = KEYS[1]
local redpackKey = KEYS[2]
local activityId = KEYS[3]
local disRedpackKey = KEYS[4]
local username = KEYS[5]
local redpackObj = redis.call('rpop', prefix..redpackKey..activityId)
local ttl = redis.call('ttl', prefix..redpackKey..activityId)
if(redpackObj ~= nil) then
-- username 添加到对象中
local obj = cjson.decode(redpackObj)
obj[2]['username']=username
local jsonObj = cjson.encode(obj)
redis.call('lpush', prefix..disRedpackKey..activityId, jsonObj)
redis.call('expire', prefix..disRedpackKey..activityId, ttl)
return jsonObj
else
return nil
end
获取id的lua脚本
local key = KEYS[1]
local id = redis.call('get', key)
if(id == false)
then
redis.call('set', key, 1)
return 1
elseif(tonumber(id) >= 999999)
then
redis.call('set', key, 1)
return '1'
else
return tostring(redis.call('incr', key))
end
抽奖的lua脚本
-- 实际也就是轮询获取随机数返回前端展示滚动数据,谁中奖前端再请求一次后台获取一次随机数
-- 从候选人列表中随机取一个,谁中奖Java中可以从list中移除这个元素
local prefix = KEYS[1]
local empKey = KEYS[2]
local length = redis.call('llen', prefix..empKey)
local index = math.random(length) -1
local obj = redis.call('lindex', prefix..empKey, index)
return obj