1. 在config中加入RedisConfig配置类
package com.nowcoder.community.config;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.RedisSerializer;@Configuration
public class RedisConfig {@Beanpublic RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(factory);template.setKeySerializer(RedisSerializer.string());template.setValueSerializer(RedisSerializer.json());template.setHashKeySerializer(RedisSerializer.json());template.setHashValueSerializer(RedisSerializer.json());template.afterPropertiesSet();return template;}
}
2. 写个测试类测试一下
package com.nowcoder.community;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.*;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;import java.util.concurrent.TimeUnit;@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = CommunityApplication.class)
public class RedisTests {@Autowiredprivate RedisTemplate redisTemplate;@Testpublic void testStrings(){String redisKey = "test:count";redisTemplate.opsForValue().set(redisKey, 1);System.out.println(redisTemplate.opsForValue().get(redisKey));System.out.println(redisTemplate.opsForValue().increment(redisKey));System.out.println(redisTemplate.opsForValue().decrement(redisKey));}@Testpublic void testHash(){String redisKey = "test:user";redisTemplate.opsForHash().put(redisKey,"id",1);redisTemplate.opsForHash().put(redisKey,"name","katniss");redisTemplate.opsForHash().put(redisKey,"age",24);System.out.println(redisTemplate.opsForHash().get(redisKey,"id"));System.out.println(redisTemplate.opsForHash().get(redisKey,"name"));System.out.println(redisTemplate.opsForHash().get(redisKey,"age"));}@Testpublic void testLists(){String redisKey = "test:ids";redisTemplate.opsForList().leftPush(redisKey,101);redisTemplate.opsForList().leftPush(redisKey,102);redisTemplate.opsForList().leftPush(redisKey,103);System.out.println(redisTemplate.opsForList().size(redisKey));System.out.println(redisTemplate.opsForList().index(redisKey,0));System.out.println(redisTemplate.opsForList().range(redisKey,0,2));System.out.println(redisTemplate.opsForList().leftPop(redisKey));System.out.println(redisTemplate.opsForList().leftPop(redisKey));System.out.println(redisTemplate.opsForList().leftPop(redisKey));}@Testpublic void testSets(){String redisKey = "test:teachers";redisTemplate.opsForSet().add(redisKey,"111","222","333","444","555");System.out.println(redisTemplate.opsForSet().size(redisKey));System.out.println(redisTemplate.opsForSet().pop(redisKey));System.out.println(redisTemplate.opsForSet().members(redisKey));}@Testpublic void testSortedSets(){String redisKey = "test:students";redisTemplate.opsForZSet().add(redisKey, "唐僧", 80);redisTemplate.opsForZSet().add(redisKey, "悟空", 90);redisTemplate.opsForZSet().add(redisKey, "八戒", 70);redisTemplate.opsForZSet().add(redisKey, "沙僧", 60);System.out.println(redisTemplate.opsForZSet().zCard(redisKey));System.out.println(redisTemplate.opsForZSet().score(redisKey,"悟空"));System.out.println(redisTemplate.opsForZSet().reverseRank(redisKey,"八戒"));System.out.println(redisTemplate.opsForZSet().reverseRange(redisKey,0,2));}@Testpublic void testKeys(){System.out.println(redisTemplate.hasKey("test:user"));redisTemplate.delete("test:user");System.out.println(redisTemplate.hasKey("test:user"));redisTemplate.expire("test:students",10, TimeUnit.SECONDS);}@Testpublic void testBoundOperations(){String redisKey = "test:count";BoundValueOperations operations = redisTemplate.boundValueOps(redisKey);operations.increment();operations.increment();operations.increment();operations.increment();operations.increment();operations.increment();System.out.println(operations.get());}@Testpublic void testTransaction(){Object obj = redisTemplate.execute(new SessionCallback() {@Overridepublic Object execute(RedisOperations operations) throws DataAccessException {String redisKey = "test:tx";operations.multi();operations.opsForSet().add(redisKey,"zhangsan");operations.opsForSet().add(redisKey,"lisi");operations.opsForSet().add(redisKey,"wangwu");System.println.out(operations.opsForSet().members(redisKey));return operations.exec();}});System.out.println(obj);}
}
3. 注意事项
- Redis不满足事务的原子性,原子性是指事务要么被全部执行,要么都不执行。但是Redis不支持回滚,就可能会出现有些语句执行成功,有些执行失败,因此具备原子性;
- Redis事务的三个阶段:
- 用Redis管理事务的时候中间不要做查询,查询会被执行但无效。例如在
testTransaction
方法中,operations.opsForSet().members(redisKey)
将返回空。