依赖引入

  1. <!-- 日志 -->
  2. <dependency>
  3. <groupId>org.slf4j</groupId>
  4. <artifactId>slf4j-api</artifactId>
  5. <version>1.7.25</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.slf4j</groupId>
  9. <artifactId>slf4j-log4j12</artifactId>
  10. <version>1.7.25</version>
  11. </dependency>
  12. <!-- redis -->
  13. <dependency>
  14. <groupId>org.springframework.data</groupId>
  15. <artifactId>spring-data-redis</artifactId>
  16. <version>1.8.0.RELEASE</version>
  17. </dependency>
  18. <dependency>
  19. <groupId>redis.clients</groupId>
  20. <artifactId>jedis</artifactId>
  21. <version>2.9.0</version>
  22. </dependency>
  23. <!--单元测试-->
  24. <dependency>
  25. <groupId>junit</groupId>
  26. <artifactId>junit</artifactId>
  27. <version>4.12</version>
  28. <scope>test</scope>
  29. </dependency>

配置文件

redis-cluster.properties

  1. redis.host1=****
  2. redis.port1=7001
  3. redis.host2=****
  4. redis.port2=7002
  5. redis.host3=****
  6. redis.port3=7003
  7. redis.host4=****
  8. redis.port4=7004
  9. redis.host5=****
  10. redis.port5=7005
  11. redis.host6=****
  12. redis.port6=7006
  13. redis.expiration=3000
  14. #最大空闲数
  15. redis.maxIdle=300
  16. #连接池的最大数据库连接数。设为0表示无限制,如果是jedis 2.4以后用redis.maxTotal
  17. redis.maxActive=600
  18. #最大建立连接等待时间
  19. redis.maxWait=1000
  20. #是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个
  21. redis.testOnBorrow=true
  22. #客户端超时时间单位是毫秒 默认是2000
  23. redis.timeout=10000
  24. #控制一个pool可分配多少个jedis实例,用来替换上面的redis.maxActive,如果是jedis 2.4以后用该属性
  25. redis.maxTotal=1000
  26. #最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。
  27. redis.maxWaitMillis=1000
  28. #连接的最小空闲时间 默认1800000毫秒(30分钟)
  29. redis.minEvictableIdleTimeMillis=300000
  30. #每次释放连接的最大数目,默认3
  31. redis.numTestsPerEvictionRun=1024
  32. #逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1
  33. redis.timeBetweenEvictionRunsMillis=30000

applocation.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5. <description>redis集群</description>
  6. <!-- 加载properties文件 -->
  7. <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  8. <property name="locations">
  9. <list>
  10. <value>classpath*:/redis-cluster.properties</value>
  11. </list>
  12. </property>
  13. </bean>
  14. <!-- 配置JedisPoolConfig实例 -->
  15. <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
  16. <!--最大空闲数-->
  17. <property name="maxIdle" value="${redis.maxIdle}" />
  18. <!--连接池的最大数据库连接数 -->
  19. <property name="maxTotal" value="${redis.maxActive}" />
  20. <!--最大建立连接等待时间-->
  21. <property name="maxWaitMillis" value="${redis.maxWait}" />
  22. <!--是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个-->
  23. <property name="testOnBorrow" value="${redis.testOnBorrow}" />
  24. <!--逐出连接的最小空闲时间 默认1800000毫秒(30分钟)-->
  25. <property name="minEvictableIdleTimeMillis" value="${redis.minEvictableIdleTimeMillis}" />
  26. <!--每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n), 默认3-->
  27. <property name="numTestsPerEvictionRun" value="${redis.numTestsPerEvictionRun}" />
  28. <!--逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1-->
  29. <property name="timeBetweenEvictionRunsMillis" value="${redis.timeBetweenEvictionRunsMillis}" />
  30. </bean>
  31. <!-- Redis集群配置 -->
  32. <bean id="redisClusterConfig" class="org.springframework.data.redis.connection.RedisClusterConfiguration">
  33. <property name="maxRedirects" value="6"></property>
  34. <property name="clusterNodes">
  35. <set>
  36. <bean class="org.springframework.data.redis.connection.RedisNode">
  37. <constructor-arg name="host" value="${redis.host1}"></constructor-arg>
  38. <constructor-arg name="port" value="${redis.port1}"></constructor-arg>
  39. </bean>
  40. <bean class="org.springframework.data.redis.connection.RedisNode">
  41. <constructor-arg name="host" value="${redis.host2}"></constructor-arg>
  42. <constructor-arg name="port" value="${redis.port2}"></constructor-arg>
  43. </bean>
  44. <bean class="org.springframework.data.redis.connection.RedisNode">
  45. <constructor-arg name="host" value="${redis.host3}"></constructor-arg>
  46. <constructor-arg name="port" value="${redis.port3}"></constructor-arg>
  47. </bean>
  48. <bean class="org.springframework.data.redis.connection.RedisNode">
  49. <constructor-arg name="host" value="${redis.host4}"></constructor-arg>
  50. <constructor-arg name="port" value="${redis.port4}"></constructor-arg>
  51. </bean>
  52. <bean class="org.springframework.data.redis.connection.RedisNode">
  53. <constructor-arg name="host" value="${redis.host5}"></constructor-arg>
  54. <constructor-arg name="port" value="${redis.port5}"></constructor-arg>
  55. </bean>
  56. <bean class="org.springframework.data.redis.connection.RedisNode">
  57. <constructor-arg name="host" value="${redis.host6}"></constructor-arg>
  58. <constructor-arg name="port" value="${redis.port6}"></constructor-arg>
  59. </bean>
  60. </set>
  61. </property>
  62. </bean>
  63. <!-- 配置JedisConnectionFactory -->
  64. <bean id="jeidsConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
  65. <constructor-arg name="clusterConfig" ref="redisClusterConfig" />
  66. <property name="poolConfig" ref="poolConfig"/>
  67. <property name="timeout" value="${redis.timeout}" />
  68. <property name="password" value="123456"></property>
  69. </bean>
  70. <!-- 配置RedisTemplate -->
  71. <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
  72. <property name="connectionFactory" ref="jeidsConnectionFactory" />
  73. <property name="keySerializer" >
  74. <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
  75. </property>
  76. <property name="valueSerializer" >
  77. <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
  78. </property>
  79. <property name="hashKeySerializer">
  80. <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
  81. </property>
  82. <property name="hashValueSerializer">
  83. <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
  84. </property>
  85. </bean>
  86. </beans>

String操作

  1. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:application.xml");
  2. RedisTemplate redisTemplate = context.getBean("redisTemplate", RedisTemplate.class);
  3. //set值
  4. redisTemplate.opsForValue().set("person:1","tom");
  5. //set值同时设置过期时间
  6. redisTemplate.opsForValue().set("person:2","bob",10, TimeUnit.SECONDS);
  7. //set void set(K key, V value, long offset);
  8. //覆写(overwrite)给定 key 所储存的字符串值,从偏移量 offset 开始
  9. redisTemplate.opsForValue().set("person:1","baby",0);
  10. //getAndSet V getAndSet(K key, V value);
  11. //设置键的字符串值并返回其旧值
  12. Object bob = redisTemplate.opsForValue().getAndSet("person:1", "bob");
  13. //Integer append(K key, String value);
  14. //如果key已存在并且是一个字符串,则该命令将该值追加到字符串末尾。如果key不存在。则被创建 并设置为空字符串
  15. redisTemplate.opsForValue().append("newKey","hello");
  16. //size Long size(K key);
  17. //返回key所对应的value值得长度
  18. redisTemplate.opsForValue().size("newKey");

List操作

  1. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:application.xml");
  2. RedisTemplate redisTemplate = context.getBean("redisTemplate", RedisTemplate.class);
  3. redisTemplate.delete("listdemo1");
  4. //Long size(K key);
  5. //返回存储在键中的列表的长度。如果键不存在,则将其解释为空列表,并返回0。当key存储的值不是列表时返回错误。
  6. Long listdemo1Size = redisTemplate.opsForList().size("listdemo1");
  7. //Long leftPush(K key, V value);
  8. //将所有指定的值插入存储在键的列表的头部。如果键不存在,则在执行推送操作之前将其创建为空列表。(左侧插值)
  9. redisTemplate.opsForList().leftPush("listdemo1","value0");
  10. //Long leftPushAll(K key, V... values);
  11. //批量把一个数组插入到列表中(批量左侧插值)
  12. redisTemplate.opsForList().leftPushAll("listdemo1", Arrays.asList("value1","value2","value3"));
  13. //Long rightPush(K key, V value);
  14. //将所有指定的值插入存储在键的列表的头部。如果键不存在,则在执行推送操作之前将其创建为空列表。(右侧插值)
  15. redisTemplate.opsForList().rightPush("listdemo1","value4");
  16. //Long rightPushAll(K key, V... values); (批量右侧插值)
  17. redisTemplate.opsForList().rightPushAll("listdemo1","value5","value6","value7");
  18. //void set(K key, long index, V value);
  19. //在列表中index的位置设置value值
  20. redisTemplate.opsForList().set("listdemo1",0,"value8");
  21. //Long remove(K key, long count, Object value);
  22. //从存储在键中的列表中删除等于value的元素的第一个计数事件。
  23. //计数参数以下列方式影响操作:
  24. //count > 0:删除等于value从头到尾移动的值的元素。
  25. //count <0:删除等于value从尾到头移动的值的元素。
  26. //count = 0:删除等于value的所有元素。
  27. redisTemplate.opsForList().remove("listdemo1",1,"value0"); ////将删除列表中存储的列表中第一次次出现的“value0”。
  28. //V index(K key, long index);
  29. //根据下表获取列表中的值,下标从0开始
  30. Object listdemo1Index = redisTemplate.opsForList().index("listdemo1", 0);
  31. //V leftPop(K key);
  32. //弹出最左边的元素,弹出之后该值在列表中将不复存在
  33. redisTemplate.opsForList().leftPop("listdemo1");
  34. //V rightPop(K key);
  35. //弹出最右边的元素,弹出之后该值在列表中将不复存在
  36. redisTemplate.opsForList().rightPop("listdemo1");
  37. System.out.println("list contains : " );
  38. redisTemplate.opsForList().range("listdemo1",0,-1).stream().forEach(obj -> System.out.println(obj));

Hash操作

  1. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:application.xml");
  2. RedisTemplate redisTemplate = context.getBean("redisTemplate", RedisTemplate.class);
  3. redisTemplate.delete("hashdemo");
  4. redisTemplate.opsForHash().put("hashdemo","name","xiaoming");
  5. redisTemplate.opsForHash().put("hashdemo","age","18");
  6. redisTemplate.opsForHash().put("hashdemo","gender","1");
  7. //Boolean hasKey(H key, Object hashKey);
  8. //确定哈希hashKey是否存在
  9. redisTemplate.opsForHash().hasKey("hashdemo","name");
  10. //HV get(H key, Object hashKey);
  11. //从键中的哈希获取给定hashKey的值
  12. redisTemplate.opsForHash().get("hashdemo","name");
  13. //Set<HK> keys(H key);
  14. //获取key所对应的散列表的key
  15. redisTemplate.opsForHash().keys("hashdemo");
  16. //Long size(H key);
  17. //获取key所对应的散列表的大小个数
  18. redisTemplate.opsForHash().size("hashdemo");
  19. // void putAll(H key, Map<? extends HK, ? extends HV> m);
  20. //使用m中提供的多个散列字段设置到key对应的散列表中
  21. Map<String,Object> testMap = new HashMap();
  22. testMap.put("name","666");
  23. testMap.put("age","27");
  24. testMap.put("class","1");
  25. redisTemplate.opsForHash().putAll("hashdemo",testMap);
  26. // void put(H key, HK hashKey, HV value);
  27. //设置散列hashKey的值
  28. redisTemplate.opsForHash().put("hashdemo","name","lisi");
  29. // List<HV> values(H key);
  30. //获取整个哈希存储的值
  31. redisTemplate.opsForHash().values("hashdemo");
  32. //Map<HK, HV> entries(H key);
  33. //获取整个哈希存储的entry
  34. redisTemplate.opsForHash().entries("hashdemo");
  35. redisTemplate.opsForHash().entries("hashdemo").entrySet().stream().forEach(entry -> System.out.println(entry));

Set操作

  1. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:application.xml");
  2. RedisTemplate redisTemplate = context.getBean("redisTemplate", RedisTemplate.class);
  3. redisTemplate.delete(Arrays.asList("setdemo1","setdemo2"));
  4. //Long add(K key, V... values);
  5. //无序集合中添加元素,返回添加个数
  6. //也可以直接在add里面添加多个值 如:template.opsForSet().add("setTest","aaa","bbb")
  7. redisTemplate.opsForSet().add("setdemo1","value0","value1","value2","value4","value5");
  8. redisTemplate.opsForSet().add("setdemo2","value1","value2","value3");
  9. //Long remove(K key, Object... values);
  10. //移除集合中一个或多个成员
  11. redisTemplate.opsForSet().remove("setdemo1","value5");
  12. //V pop(K key);
  13. //移除并返回集合中的一个随机元素
  14. redisTemplate.opsForSet().pop("setdemo1");
  15. // Boolean move(K source, V value, K destination);
  16. //将 member 元素从 source 集合移动到 destination 集合
  17. redisTemplate.opsForSet().move("setdemo1","value1","setdemo2");
  18. //Long size(K key);
  19. //无序集合的大小长度
  20. redisTemplate.opsForSet().size("setdemo1");
  21. //Set<V> members(K key);
  22. //返回集合中的所有成员
  23. redisTemplate.opsForSet().members("setdemo1");
  24. System.out.println("setdemo1");
  25. redisTemplate.opsForSet().members("setdemo1").stream().forEach(entry -> System.out.print(entry+";"));
  26. System.out.println();
  27. System.out.println("setdemo2");
  28. redisTemplate.opsForSet().members("setdemo2").stream().forEach(entry -> System.out.print(entry+";"));
  29. System.out.println();

Zset操作

  1. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:application.xml");
  2. RedisTemplate redisTemplate = context.getBean("redisTemplate", RedisTemplate.class);
  3. //Boolean add(K key, V value, double score);
  4. //新增一个有序集合,存在的话为false,不存在的话为true
  5. redisTemplate.opsForZSet().add("zsetdemo","value1",100);
  6. redisTemplate.opsForZSet().add("zsetdemo","value2",200);
  7. redisTemplate.opsForZSet().add("zsetdemo","value3",300);
  8. redisTemplate.opsForZSet().add("zsetdemo","value3ForRemove",300);
  9. //Long remove(K key, Object... values);
  10. //从有序集合中移除一个或者多个元素
  11. redisTemplate.opsForZSet().remove("zsetdemo","value3ForRemove");
  12. // Long rank(K key, Object o);
  13. //返回有序集中指定成员的排名,其中有序集成员按分数值递增(从小到大)顺序排列
  14. redisTemplate.opsForZSet().rank("zsetdemo","value2");
  15. //Set<V> range(K key, long start, long end);
  16. //通过索引区间返回有序集合成指定区间内的成员,其中有序集成员按分数值递增(从小到大)顺序排列
  17. redisTemplate.opsForZSet().range("zsetdemo",0,-1);
  18. //Long count(K key, double min, double max);
  19. //通过分数返回有序集合指定区间内的成员个数
  20. Set zsetdemo = redisTemplate.opsForZSet().rangeByScore("zsetdemo", 0, 300);
  21. //Double score(K key, Object o);
  22. //获取指定成员的score值
  23. Double score = redisTemplate.opsForZSet().score("zsetdemo", "value2");
  24. //Long removeRange(K key, long start, long end);
  25. //移除指定索引位置的成员,其中有序集成员按分数值递增(从小到大)顺序排列
  26. redisTemplate.opsForZSet().remove("zsetdemo",0,1);