最近使用redis遇到一些问题,记录一下。
格式就不排了,有时间再弄吧。
一、版本信息
spring 4.2.5
mysql 5.1.18
mybatis 3.2.8
mybatis-spring 1.2.4
jedis 2.8.0
spring-data 1.7.1
... ...
二、jedis和spring集成
1. 配置数据源
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.maxTotal}" />
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="minIdle" value="${redis.minIdle}" />
<property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
<!--对拿到的connection进行validateObject校验 -->
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
<!--在进行returnObject对返回的connection进行validateObject校验 -->
<property name="testOnReturn" value="${redis.testOnReturn}" />
<!--定时对线程池中空闲的链接进行validateObject校验 -->
<property name="testWhileIdle" value="${redis.testWhileIdle}" />
</bean>
<bean id="redisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.host}"/>
<property name="port" value="${redis.port}"/>
<property name="password" value="${redis.password}" />
<property name="timeout" value="${redis.timeout}" />
<property name="database" value="${redis.default.database}" />
<property name="poolConfig" ref="jedisPoolConfig"/>
</bean>
<bean id="redis4CacheConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.host}"/>
<property name="port" value="${redis.port}"/>
<property name="password" value="${redis.password}" />
<property name="timeout" value="${redis.timeout}" />
<property name="database" value="${redis.cache.database}" />
<property name="poolConfig" ref="jedisPoolConfig"/>
</bean>
2.配置Template
<bean id="stringRedisSerializer"
class="org.springframework.data.redis.serializer.StringRedisSerializer" />
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="redisConnectionFactory" />
<property name="keySerializer" ref="stringRedisSerializer" />
<property name="hashKeySerializer" ref="stringRedisSerializer" />
<!-- 默认即为 jdkRedisSerializer -->
<!-- <property name="valueSerializer" ref="jdkRedisSerializer" />
<property name="hashValueSerializer" ref="jdkRedisSerializer" /> -->
<!-- <property name="enableTransactionSupport" value="true" /> -->
</bean>
注:在spring事务中不执行Lua脚本可使用enableTransactionSupport,否则报java.lang.unsupportedclassversion
三、与springCache集成
<bean id="redis4CacheTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="redis4CacheConnectionFactory" />
<property name="keySerializer" ref="stringRedisSerializer" />
</bean>
<!-- 开启缓存注解 -->
<cache:annotation-driven/>
<!-- declare Redis Cache Manager -->
<bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
<constructor-arg name="redisOperations" ref="redis4CacheTemplate" />
<!-- 是否事务提交,如果事务回滚,缓存也回滚,默认false -->
<property name="transactionAware" value="true" />
<!-- 默认有效期1d 3600*24 -->
<property name="defaultExpiration" value="86400" />
</bean>
public abstract class BaseCache {
public String generateKey(String... args) {
return RedisUtil.generateKey(args);
}
@CacheEvict(allEntries = true)
public abstract void clearCaches(Class<?> clazz);
}
@Component(ActivityCache.SERVICE_NAME)
@CacheConfig(cacheNames = {ActivityCache.SERVICE_NAME})
public class ActivityCache extends BaseCache {
/**
* SERVICE_NAME:服务名
*/
public static final String SERVICE_NAME = "activityCache";
@Autowired
private ActivityDao activityDao;
@Cacheable(key="#root.target.generateKey(#root.targetClass.getSimpleName(), #activityCode)",
unless="#result == null")
public Activity getActivityInfo(String activityCode){
Activity activity = null;
try {
} catch (Exception e) {
log.error(e.getMessage(),e);
}
return activity;
}
@CachePut(key="#root.target.generateKey(#root.targetClass.getSimpleName(), #activity.activityCode)",
unless="#result == null")
public Activity saveOrUpdateActivityInfo(Activity activity)
@CachePut(key="#root.target.generateKey(#root.targetClass.getSimpleName(), #activityCode)",
unless="#result == null")
public int delActivity(String activityCode)
@CacheEvict(allEntries = true)
public void clearCaches(Class<?> clazz)
}
四、关于取值异常问题
Cannot serialize; nested exception is org.springframework.core.serializer.support.SerializationFailedException: Failed to serialize object using DefaultSerializer; nested exception is java.io.NotSerializableException
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:spring/spring.xml"
// ,
// "classpath:spring/spring-mvc.xml",
// "file:src/main/java/com/sinowel/eacpc/**/mapping/*.xml"
})
public class TestBase {
@Before
public void setUp() throws Exception {
}
}
public class TestLua2 extends TestBase {
private final Logger logger = LoggerFactory.getLogger(TestLua2.class);
@Resource(name = RedisService.SERVICE_NAME)
private RedisService redisService; // redis
@Resource(name = "redisTemplate")
private RedisTemplate<String, Object> redisTemplate; // redis
@Test
public void testInject() throws Exception {
// 在服务器redis-cli连接预存入Hash key=tt1 field=t1,t2的数值
String redisKey = "tt1";
List<String> fieldList = Arrays.asList(new String[] {"t1", "t2"});
List<Long> list = redisService.getMultiHash(redisKey, fieldList, null);
System.out.println(list.get(0).longValue());
// 存入时使用stringRedisSerializer,取值时使用jdkRedisSerializer。xml RedisTemplate时配置
// 异常,先存,再次取,配置文件切换配置
String redisKey = "tt2";
List<String> fieldList = Arrays.asList(new String[] {"t1", "t2"});
redisService.setHash(redisKey, "t1", "1");
redisService.setHash(redisKey, "t2", "2");
BoundHashOperations<String, String, Long> bhOps = redisTemplate
.boundHashOps(redisKey);
List<Long> list = bhOps.multiGet(fieldList);
System.out.println(list.get(0));
// keySerializer 和 hashKeySerializer 设置 stringRedisSerializer
// valueSerializer 和 hashValueSerializer 设置成默认 jdkRedisSerializer
// 则可以取得指定值,否则都使用stringRedisSerializer,取到的值一定是String类型
String redisKey = "tt3";
List<String> fieldList = Arrays.asList(new String[] {"t1", "t2"});
BoundHashOperations<String, String, Long> bhOps = redisTemplate
.boundHashOps(redisKey);
bhOps.put("t1", 1L);
bhOps.put("t2", 2L);
List<Long> list = bhOps.multiGet(fieldList);
System.out.println(list.get(0));
}
}
redis-cli取到序列化后的值【1】
stringRedisSerializer:
"\xac\xed\x00\x05t\x00\x011"
jdkRedisSerializer:
"\xac\xed\x00\x05sr\x00\x11java.lang.Integer\x12\xe2\xa0\xa4\xf7\x81\x878\x02\x00\x01I\x00\x05valuexr\x00\x10java.lang.Number\x86\xac\x95\x1d\x0b\x94\xe0\x8b\x02\x00\x00xp\x00\x00\x00\x01"
总结:各个RedisSerializer序列化的值存入redis后,取值时,是不能切换取值的。因为序列化方式不同。
分享到:
相关推荐
在Spring体系中使用redis.zip spring集成redis缓存
redis整合spring
SpringCloud整合Redis缓存;版本:SpringCloud :Dalston.SR4;SpringBoot:1.5.14.RELEASE;Redis:3.2.0;Maven :3.5.3.代码下载下来即可用
spring-data-redis-1.6.2.RELEASE.jar spring与redis集成包 commons-pool2-2.4.2.jar 与redis连接池 spring-data-commons-2.0.0.RC2.jar spring数据包 redis-context.xml redis和spring配置,需要引用到自己项目的...
spring和redis集成有很多方式,看到网上很多都是使用redistemplate自己去做redis 的一些操作,但是对于我们开发来说,肯定是使用越方便越好,于是乎就有了spring的对redis或者memcahe这些换成框架的封装,只需要引入...
Spring集成redis集群
里面写了对Redis库的增删该查操作,对列表和批量添加删除进行了介绍,如何进行批量添加以提升性能
整合了spring和redis相关jar,具体使用方法和代码请查看我的博客。
redis整合spring,redis集群
redis集成spring实现aop.zip redis集成spring实现aop.zip
Redis+spring-data-redis jar包Redis+spring-data-redis jar包Redis+spring-data-redis jar包
redis与springcache集成,对应的文章可以在我的csdn博客中找到
Spring + redis集群的集成 spring-data-redis-1.8.1.RELEASE.jar jedis-2.9.0.jar spring-data-commons-1.8.1.RELEASE.jar commons-pool2-2.4.2.jar
spring中使用redis,将想要缓存的信息存入redis中,想取的时候取出来,想删的时候删除掉。
spring mvc 集成redis使用例子,代码在com.kevin.server. 测试入口在:RedisAnnotationTest.java 和 RedisTest.java
Spring mvc整合redis实例(redis连接池) 将所有jedis 增删改进行事务的封装、查询单独做为有返回参数的封装。简化了很多代码
springBoot+mybatis+redis集成,不上代码要确保redis服务端已经运行
springboot 使用spring cache缓存 和 使用fastjson配置redis系列化,springboot 使用spring cache缓存 和 使用fastjson配置redis系列化,springboot 使用spring cache缓存 和 使用fastjson配置redis系列化,springboot ...
SpringMvc集成Redis项目完整示例,包含web示例和java示例(不用启动web服务器)即可测试,亲测没问题,本人项目中已经使用,欢迎有问题一起探讨!
SSH+Redis整合