乱码问题解决序列化方式 (所有的对象都需要序列化不然会报错)
1,使用 objectmapper().writevalueasstring(*);
void textchanged() throws jsonprocessingexception {
user user = new user("张三", 22);
string s = new objectmapper().writevalueasstring(user);
redistemplate.opsforvalue().set("user",s);
}2,所有pojo实现 serializable接口
@allargsconstructor
@data
@noargsconstructor
@component
public class user implements serializable {
private string username;
private int age;
}
3,自定义序列化,企业开发中可以直接使用
package com.example.config;
import com.fasterxml.jackson.annotation.jsonautodetect;
import com.fasterxml.jackson.annotation.propertyaccessor;
import com.fasterxml.jackson.databind.objectmapper;
import org.springframework.boot.autoconfigure.condition.conditionalonmissingbean;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
import org.springframework.data.redis.connection.redisconnectionfactory;
import org.springframework.data.redis.core.redistemplate;
import org.springframework.data.redis.serializer.jackson2jsonredisserializer;
import org.springframework.data.redis.serializer.stringredisserializer;
import java.net.unknownhostexception;
@configuration
public class redisconfig {
//编写自己的redsitemplate
@bean
@suppresswarnings("all")
public redistemplate<string, object> redistemplate(redisconnectionfactory factory) {
redistemplate<string, object> template = new redistemplate<string, object>();
template.setconnectionfactory(factory);
// 序列化配置 解析任意对象
jackson2jsonredisserializer jackson2jsonredisserializer = new jackson2jsonredisserializer(object.class);
// json序列化利用objectmapper进行转义
objectmapper om = new objectmapper();
om.setvisibility(propertyaccessor.all, jsonautodetect.visibility.any);
om.enabledefaulttyping(objectmapper.defaulttyping.non_final);
jackson2jsonredisserializer.setobjectmapper(om);
// 2.序列化string类型
stringredisserializer stringredisserializer = new stringredisserializer();
// key采用string的序列化方式
template.setkeyserializer(stringredisserializer);
// hash的key也采用string的序列化方式
template.sethashkeyserializer(stringredisserializer);
// value序列化方式采用jackson
template.setvalueserializer(jackson2jsonredisserializer);
// hash的value序列化方式采用jackson
template.sethashvalueserializer(jackson2jsonredisserializer);
template.afterpropertiesset();
return template;
}
}
我要保证使用到了我们自定义的redistemplate 使用添加注解 当可以点进去 即为成功!
@qualifier("redistemplate")@autowired
@qualifier("redistemplate")
private redistemplate redistemplate;这样一来,只要实体类进行了序列化,我们存什么都不会有乱码的担忧了。
自定义redis工具类
使用redistemplate需要频繁调用
.opforxxx然后才能进行对应的操作,这样使用起来代码效率低下,工作中一般不会这样使用,而是将这些常用的公共api抽取出来封装成为一个工具类,然后直接使用工具类来间接操作redis,不但效率高并且易用。
package com.demo.utils;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.data.redis.core.redistemplate;
import org.springframework.stereotype.component;
import org.springframework.util.collectionutils;
import java.util.list;
import java.util.map;
import java.util.set;
import java.util.concurrent.timeunit;
/**
redistemplate封装
*/
@component
public class redisutils {
//此写法可防止redistemplate 注入失败
private static redistemplate redistemplate;
@autowired
public void setredistemplate(redistemplate redistemplate) {
redisutils.redistemplate = redistemplate;
}
/**
指定缓存失效时间
@param key 键
@param time 时间(秒)
@return
*/
public boolean expire(string key,long time){
try {
if(time>0){
redistemplate.expire(key, time, timeunit.seconds);
}
return true;
} catch (exception e) {
e.printstacktrace();
return false;
}
}
/**
根据key 获取过期时间
@param key 键 不能为null
@return 时间(秒) 返回0代表为永久有效
*/
public long getexpire(string key){
return redistemplate.getexpire(key,timeunit.seconds);
}
/**
判断key是否存在
@param key 键
@return true 存在 false不存在
*/
public boolean haskey(string key){
try {
return redistemplate.haskey(key);
} catch (exception e) {
e.printstacktrace();
return false;
}
}
/**
删除缓存
@param key 可以传一个值 或多个
*/
@suppresswarnings("unchecked")
public void del(string[] key){
if(key!=null&&key.length>0){
if(key.length==1){
redistemplate.delete(key[0]);
}else{
redistemplate.delete(collectionutils.arraytolist(key));
}
}
}
//string=
/**
普通缓存获取
@param key 键
@return 值
*/
public object get(string key){
return key==null?null:redistemplate.opsforvalue().get(key);
}
/**
普通缓存放入
@param key 键
@param value 值
@return true成功 false失败
*/
public static boolean set(string key,object value) {
try {
redistemplate.opsforvalue().set(key, value);
return true;
} catch (exception e) {
e.printstacktrace();
return false;
}
}
/**
普通缓存放入并设置时间
@param key 键
@param value 值
@param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期
@return true成功 false 失败
*/
public boolean set(string key,object value,long time){
try {
if(time>0){
redistemplate.opsforvalue().set(key, value, time, timeunit.seconds);
}else{
set(key, value);
}
return true;
} catch (exception e) {
e.printstacktrace();
return false;
}
}
/**
递增
@param key 键
@param delta 要增加几(大于0)
@return
*/
public long incr(string key, long delta){
if(delta<0){
throw new runtimeexception("递增因子必须大于0");
}
return redistemplate.opsforvalue().increment(key, delta);
}
/**
递减
@param key 键
@param delta 要减少几(小于0)
@return
*/
public long decr(string key, long delta){
if(delta<0){
throw new runtimeexception("递减因子必须大于0");
}
return redistemplate.opsforvalue().increment(key, -delta);
}
//map=
/**
hashget
@param key 键 不能为null
@param item 项 不能为null
@return 值
*/
public object hget(string key,string item){
return redistemplate.opsforhash().get(key, item);
}
/**
获取hashkey对应的所有键值
@param key 键
@return 对应的多个键值
*/
public map<object,object> hmget(string key){
return redistemplate.opsforhash().entries(key);
}
/**
hashset
@param key 键
@param map 对应多个键值
@return true 成功 false 失败
*/
public boolean hmset(string key, map<string,object> map){
try {
redistemplate.opsforhash().putall(key, map);
return true;
} catch (exception e) {
e.printstacktrace();
return false;
}
}
/**
hashset 并设置时间
@param key 键
@param map 对应多个键值
@param time 时间(秒)
@return true成功 false失败
*/
public boolean hmset(string key, map<string,object> map, long time){
try {
redistemplate.opsforhash().putall(key, map);
if(time>0){
expire(key, time);
}
return true;
} catch (exception e) {
e.printstacktrace();
return false;
}
}
/**
向一张hash表中放入数据,如果不存在将创建
@param key 键
@param item 项
@param value 值
@return true 成功 false失败
*/
public boolean hset(string key,string item,object value) {
try {
redistemplate.opsforhash().put(key, item, value);
return true;
} catch (exception e) {
e.printstacktrace();
return false;
}
}
/**
向一张hash表中放入数据,如果不存在将创建
@param key 键
@param item 项
@param value 值
@param time 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间
@return true 成功 false失败
*/
public boolean hset(string key,string item,object value,long time) {
try {
redistemplate.opsforhash().put(key, item, value);
if(time>0){
expire(key, time);
}
return true;
} catch (exception e) {
e.printstacktrace();
return false;
}
}
/**
删除hash表中的值
@param key 键 不能为null
@param item 项 可以使多个 不能为null
*/
public void hdel(string key, object item){
redistemplate.opsforhash().delete(key,item);
}
/**
判断hash表中是否有该项的值
@param key 键 不能为null
@param item 项 不能为null
@return true 存在 false不存在
*/
public boolean hhaskey(string key, string item){
return redistemplate.opsforhash().haskey(key, item);
}
/**
hash递增 如果不存在,就会创建一个 并把新增后的值返回
@param key 键
@param item 项
@param by 要增加几(大于0)
@return
*/
public double hincr(string key, string item,double by){
return redistemplate.opsforhash().increment(key, item, by);
}
/**
hash递减
@param key 键
@param item 项
@param by 要减少记(小于0)
@return
*/
public double hdecr(string key, string item,double by){
return redistemplate.opsforhash().increment(key, item,-by);
}
//set=
/**
根据key获取set中的所有值
@param key 键
@return
*/
public set sget(string key){
try {
return redistemplate.opsforset().members(key);
} catch (exception e) {
e.printstacktrace();
return null;
}
}
/**
根据value从一个set中查询,是否存在
@param key 键
@param value 值
@return true 存在 false不存在
*/
public boolean shaskey(string key,object value){
try {
return redistemplate.opsforset().ismember(key, value);
} catch (exception e) {
e.printstacktrace();
return false;
}
}
/**
将数据放入set缓存
@param key 键
@param values 值 可以是多个
@return 成功个数
*/
public long sset(string key, object values) {
try {
return redistemplate.opsforset().add(key, values);
} catch (exception e) {
e.printstacktrace();
return 0;
}
}
/**
将set数据放入缓存
@param key 键
@param time 时间(秒)
@param values 值 可以是多个
@return 成功个数
*/
public long ssetandtime(string key,long time,object values) {
try {
long count = redistemplate.opsforset().add(key, values);
if(time>0) {
expire(key, time);
}
return count;
} catch (exception e) {
e.printstacktrace();
return 0;
}
}
/**
获取set缓存的长度
@param key 键
@return
*/
public long sgetsetsize(string key){
try {
return redistemplate.opsforset().size(key);
} catch (exception e) {
e.printstacktrace();
return 0;
}
}
/**
移除值为value的
@param key 键
@param values 值 可以是多个
@return 移除的个数
*/
public long setremove(string key, object values) {
try {
long count = redistemplate.opsforset().remove(key, values);
return count;
} catch (exception e) {
e.printstacktrace();
return 0;
}
}
//=list===
/**
获取list缓存的内容
@param key 键
@param start 开始
@param end 结束 0 到 -1代表所有值
@return
*/
public list lget(string key, long start, long end){
try {
return redistemplate.opsforlist().range(key, start, end);
} catch (exception e) {
e.printstacktrace();
return null;
}
}
/**
获取list缓存的长度
@param key 键
@return
*/
public long lgetlistsize(string key){
try {
return redistemplate.opsforlist().size(key);
} catch (exception e) {
e.printstacktrace();
return 0;
}
}
/**
通过索引 获取list中的值
@param key 键
@param index 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推
@return
*/
public object lgetindex(string key,long index){
try {
return redistemplate.opsforlist().index(key, index);
} catch (exception e) {
e.printstacktrace();
return null;
}
}
/**
将list放入缓存
@param key 键
@param value 值
@return
*/
public boolean lset(string key, object value) {
try {
redistemplate.opsforlist().rightpush(key, value);
return true;
} catch (exception e) {
e.printstacktrace();
return false;
}
}
/**
将list放入缓存
@param key 键
@param value 值
@param time 时间(秒)
@return
*/
public boolean lset(string key, object value, long time) {
try {
redistemplate.opsforlist().rightpush(key, value);
if (time > 0) {
expire(key, time);
}
return true;
} catch (exception e) {
e.printstacktrace();
return false;
}
}
/**
将list放入缓存
@param key 键
@param value 值
@return
*/
public boolean lset(string key, list value) {
try {
redistemplate.opsforlist().rightpushall(key, value);
return true;
} catch (exception e) {
e.printstacktrace();
return false;
}
}
/**
将list放入缓存
@param key 键
@param value 值
@param time 时间(秒)
@return
*/
public boolean lset(string key, list value, long time) {
try {
redistemplate.opsforlist().rightpushall(key, value);
if (time > 0) {
expire(key, time);
}
return true;
} catch (exception e) {
e.printstacktrace();
return false;
}
}
/**
根据索引修改list中的某条数据
@param key 键
@param index 索引
@param value 值
@return
*/
public boolean lupdateindex(string key, long index,object value) {
try {
redistemplate.opsforlist().set(key, index, value);
return true;
} catch (exception e) {
e.printstacktrace();
return false;
}
}
/**
移除n个值为value
@param key 键
@param count 移除多少个
@param value 值
@return 移除的个数
*/
public long lremove(string key,long count,object value) {
try {
long remove = redistemplate.opsforlist().remove(key, count, value);
return remove;
} catch (exception e) {
e.printstacktrace();
return 0;
}
}
}到此这篇关于reids自定义redistemplate以及乱码问题解决的文章就介绍到这了,更多相关reids自定义redistemplate乱码内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论