SpringDataRedis的详细使用

落日映苍穹つ 2021-12-04 05:13 476阅读 0赞

准备工作

1、引入 JedisSpringDataRedis依赖。

  1. <dependency>
  2. <groupId>redis.clients</groupId>
  3. <artifactId>jedis</artifactId>
  4. <version>2.9.0</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.data</groupId>
  8. <artifactId>spring-data-redis</artifactId>
  9. <version>2.1.3.RELEASE</version>
  10. </dependency>

2、redis.properties文件

  1. redis.host=127.0.0.1
  2. redis.port=6379
  3. redis.pass=
  4. redis.database=0
  5. redis.maxIdle=300
  6. redis.maxWait=3000
  7. redis.testOnBorrow=true

3、spring-redis.xml文件

  1. <context:property-placeholder location="classpath*:properties/redis-config.properties" />
  2. <!-- redis 相关配置 -->
  3. <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
  4. <property name="maxIdle" value="${redis.maxIdle}" />
  5. <property name="maxWaitMillis" value="${redis.maxWait}" />
  6. <property name="testOnBorrow" value="${redis.testOnBorrow}" />
  7. </bean>
  8. <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig" />
  9. <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
  10. <property name="connectionFactory" ref="jedisConnectionFactory" />
  11. </bean>

maxIdle :最大空闲数
maxWaitMillis:连接时的最大等待毫秒数
testOnBorrow:在提取一个jedis实例时,是否提前进行验证操作;如果为true,则得到Jedis实例均是可用的

基本使用

值类型
  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @ContextConfiguration(locations = { "classpath:spring/spring-redis.xml"})
  3. public class ValueTest{
  4. @Autowired
  5. private RedisTemplate<String,Object> redisTemplate;
  6. /** * 存入值 */
  7. @Test
  8. public void setValue() {
  9. redisTemplate.boundValueOps("name").set("懒猴子CG");
  10. System.out.println("缓存存入成功!");
  11. }
  12. /** * 获取值 */
  13. @Test
  14. public void getValue() {
  15. String name = (String) redisTemplate.boundValueOps("name").get();
  16. System.out.println("缓存:" + name);
  17. }
  18. /** * 删除值 */
  19. @Test
  20. public void deleteValue() {
  21. redisTemplate.delete("name");
  22. System.out.println("缓存删除成功!");
  23. }
  24. }
Set类型
  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @ContextConfiguration(locations = { "classpath:spring/spring-redis.xml"})
  3. public class SetTest {
  4. @Autowired
  5. private RedisTemplate<String,Object> redisTemplate;
  6. /** * 存入值 */
  7. @Test
  8. public void setValue(){
  9. redisTemplate.boundSetOps("NBASet").add("欧文");
  10. redisTemplate.boundSetOps("NBASet").add("哈登");
  11. redisTemplate.boundSetOps("NBASet").add("乔治");
  12. }
  13. /** * 获取值 */
  14. @Test
  15. public void getValue(){
  16. Set members = redisTemplate.boundSetOps("NBASet").members();
  17. System.out.println(members);
  18. }
  19. /** * 删除某值 */
  20. @Test
  21. public void deleteValue(){
  22. redisTemplate.boundSetOps("NBASet").remove("哈登");
  23. }
  24. /** * 删除所有值 */
  25. @Test
  26. public void deleteAllValue(){
  27. redisTemplate.delete("NBASet");
  28. }
  29. }
List类型
  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @ContextConfiguration(locations = { "classpath:spring/spring-redis.xml"})
  3. public class ListTest {
  4. @Autowired
  5. private RedisTemplate<String,Object> redisTemplate;
  6. /** * 右压栈:后添加的对象排在后边 */
  7. @Test
  8. public void testSetRightValue(){
  9. redisTemplate.boundListOps("NBAList").rightPush("欧文");
  10. redisTemplate.boundListOps("NBAList").rightPush("哈登");
  11. redisTemplate.boundListOps("NBAList").rightPush("乔治");
  12. }
  13. /** * 显示右压栈集合 */
  14. @Test
  15. public void testGetRightValue(){
  16. List list = redisTemplate.boundListOps("NBAList").range(0, 10);
  17. System.out.println(list);
  18. }
  19. /** * 左压栈:后添加的对象排在前边 */
  20. @Test
  21. public void testSetLeftValue(){
  22. redisTemplate.boundListOps("NBAList").leftPush("欧文");
  23. redisTemplate.boundListOps("NBAList").leftPush("哈登");
  24. redisTemplate.boundListOps("NBAList").leftPush("乔治");
  25. }
  26. /** * 显示左压栈集合 */
  27. @Test
  28. public void testGetLeftValue(){
  29. List list = redisTemplate.boundListOps("NBAList").range(0, 10);
  30. System.out.println(list);
  31. }
  32. /** * 查询集合某个元素 */
  33. @Test
  34. public void testSearchByIndex(){
  35. String s = (String) redisTemplate.boundListOps("NBAList").index(3);
  36. System.out.println(s);
  37. }
  38. /** * 移除集合某个元素 */
  39. @Test
  40. public void testRemoveByIndex() {
  41. redisTemplate.boundListOps("NBAList").remove(1, "哈登");
  42. }
  43. }
Hash类型
  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @ContextConfiguration(locations = { "classpath:spring/spring-redis.xml"})
  3. public class HashTest {
  4. @Autowired
  5. private RedisTemplate<String,Object> redisTemplate;
  6. /** * 存入值 */
  7. @Test
  8. public void testSetValue(){
  9. redisTemplate.boundHashOps("NBAHash").put("OW", "欧文");
  10. redisTemplate.boundHashOps("NBAHash").put("HD", "哈登");
  11. redisTemplate.boundHashOps("NBAHash").put("QZ", "乔治");
  12. }
  13. /** * 获取所有key */
  14. @Test
  15. public void testGetKeys(){
  16. Set s = redisTemplate.boundHashOps("NBAHash").keys();
  17. System.out.println(s);
  18. }
  19. /** * 获取所有value */
  20. @Test
  21. public void testGetValues(){
  22. List values = redisTemplate.boundHashOps("NBAHash").values();
  23. System.out.println(values);
  24. }
  25. /** * 根据key取值 */
  26. @Test
  27. public void testGetValueByKey(){
  28. String value = (String) redisTemplate.boundHashOps("NBAHash").get("OW");
  29. System.out.println(value);
  30. }
  31. /** * 根据key删值 */
  32. @Test
  33. public void testRemoveValueByKey(){
  34. redisTemplate.boundHashOps("NBAHash").delete("QZ");
  35. }
  36. }
(完)有问题欢迎留言。

发表评论

表情:
评论列表 (有 0 条评论,476人围观)

还没有评论,来说两句吧...

相关阅读

    相关 SpringDataRedis介绍

      实际项目中我们一般都会使用Redis来作为我们的缓存组件,往往又会和Spring一块使用,虽然Redis官方提供的有Jedis等客户端工具,但是使用的时候还是有些不方便,这