栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot项目里面使用redis出现看不懂的xacxedx00x05tx00解决方法(规范使用)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot项目里面使用redis出现看不懂的xacxedx00x05tx00解决方法(规范使用)

1.redis配置

	import com.alibaba.fastjson.parser.ParserConfig;
	import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
	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.StringRedisSerializer;
	@Configuration
	public class RedisConfig {
	    @Bean
	    public RedisTemplate redisTemplate(RedisConnectionFactory  redisConnectionFactory) {
	
	        RedisTemplate redisTemplate = new RedisTemplate();
	        redisTemplate.setConnectionFactory(redisConnectionFactory);
	        FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class);
	        ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
	        redisTemplate.setValueSerializer(fastJsonRedisSerializer);
	        redisTemplate.setHashValueSerializer(fastJsonRedisSerializer);
	        redisTemplate.setKeySerializer(new StringRedisSerializer());
	        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
	        redisTemplate.afterPropertiesSet();
	
	        return redisTemplate;
	    }
	}
 

2.redis工具类

	import org.springframework.beans.factory.annotation.Autowired;
	import org.springframework.data.redis.core.BoundSetOperations;
	import org.springframework.data.redis.core.HashOperations;
	import org.springframework.data.redis.core.RedisTemplate;
	import org.springframework.data.redis.core.ValueOperations;
	import org.springframework.stereotype.Component;
	import java.util.*;
	import java.util.concurrent.TimeUnit;
	
	
	@SuppressWarnings(value = {"unchecked", "rawtypes"})
	@Component
	public class RedisService {
	    @Autowired
	    public RedisTemplate redisTemplate;
	
	    
	    public  void setCacheObject(final String key, final T value) {
	        redisTemplate.opsForValue().set(key, value);
	    }
	
	    
	    public  void setCacheObject(final String key, final T value, final Long timeout, final TimeUnit timeUnit) {
	        redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
	    }
	
	    
	    public boolean expire(final String key, final long timeout) {
	        return expire(key, timeout, TimeUnit.SECONDS);
	    }
	
	    
	    public boolean expire(final String key, final long timeout, final TimeUnit unit) {
	        return redisTemplate.expire(key, timeout, unit);
	    }
	
	    
	    public long getExpire(final String key) {
	        return redisTemplate.getExpire(key);
	    }
	
	    
	    public Boolean hasKey(String key) {
	        return redisTemplate.hasKey(key);
	    }
	
	    
	    public  T getCacheObject(final String key) {
	        ValueOperations operation = redisTemplate.opsForValue();
	        return operation.get(key);
	    }
	
	    
	    public boolean deleteObject(final String key) {
	        return redisTemplate.delete(key);
	    }
	
	    
	    public long deleteObject(final Collection collection) {
	        return redisTemplate.delete(collection);
	    }
	
	    
	    public  long setCacheList(final String key, final List dataList) {
	        Long count = redisTemplate.opsForList().rightPushAll(key, dataList);
	        return count == null ? 0 : count;
	    }
	
	    
	    public  List getCacheList(final String key) {
	        return redisTemplate.opsForList().range(key, 0, -1);
	    }
	
	    
	    public  BoundSetOperations setCacheSet(final String key, final Set dataSet) {
	        BoundSetOperations setOperation = redisTemplate.boundSetOps(key);
	        Iterator it = dataSet.iterator();
	        while (it.hasNext()) {
	            setOperation.add(it.next());
	        }
	        return setOperation;
	    }
	
	    
	    public  Set getCacheSet(final String key) {
	        return redisTemplate.opsForSet().members(key);
	    }
	
	    
	    public  void setCacheMap(final String key, final Map dataMap) {
	        if (dataMap != null) {
	            redisTemplate.opsForHash().putAll(key, dataMap);
	        }
	    }
	
	    
	    public  Map getCacheMap(final String key) {
	        return redisTemplate.opsForHash().entries(key);
	    }
	
	    
	    public  void setCacheMapValue(final String key, final String hKey, final T value) {
	        redisTemplate.opsForHash().put(key, hKey, value);
	    }
	
	    
	    public  T getCacheMapValue(final String key, final String hKey) {
	        HashOperations opsForHash = redisTemplate.opsForHash();
	        return opsForHash.get(key, hKey);
	    }
	
	    
	    public  List getMultiCacheMapValue(final String key, final Collection hKeys) {
	        return redisTemplate.opsForHash().multiGet(key, hKeys);
	    }
	
	    
	    public Collection keys(final String pattern) {
	        return redisTemplate.keys(pattern);
	    }
	}
 

3.存储方式:

3.1 图上第一种存储方式,显然比第二种查找方便,存储时使用字符串+冒号(文件夹分类存储),至于前面出现的xacxed字符串是因为序列化和反序列化导致,加上第一点配置即可

    redisService.setCacheObject("login_tokens:"+ uuid,JSON.toJSONString(user),
            CacheConstants.EXPIRATION, TimeUnit.MINUTES);
转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号