Springboot集成redis

springboot集成redis文档:
spring-boot-starter-data-redis文档

Maven依赖:

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

注入对象:

 @Autowired
    protected StringRedisTemplate stringRedisTemplate;
 @Autowired
    protected RedisTemplate redisTemplate;

核心代码:

@Component
public class BaseRedisService {

    @Autowired
    protected StringRedisTemplate stringRedisTemplate;

    /**
     * 存入键值时间
     * @param key
     * @param data
     * @param dataTime
     */
    public void setString(String key,String data,Long dataTime){
            if(data instanceof String){
                String value=(String)data;
                stringRedisTemplate.opsForValue().set(key,value);
            }
            if(null!=dataTime){
                stringRedisTemplate.expire(key,dataTime, TimeUnit.SECONDS);
            }
    }

    /**
     * 获取key
     * @param key
     * @return
     */
    public Object getString(String key){

        String s = stringRedisTemplate.opsForValue().get(key);
        return s;
    }
}

两者StringRedisTemplate —RedisTemplate:

通过RedisTemplate处理对象

大多数用户可能会使用RedisTemplate及其相应的软件包org.springframework.data.redis.core。实际上,由于模板具有丰富的功能集,因此它是Redis模块的中心类。该模板为Redis交互提供了高级抽象。虽然RedisConnection提供了接受和返回二进制值(byte数组)的低级方法,但是模板负责序列化和连接管理,使用户无需处理此类细节。
此外,该模板提供了操作视图(根据Redis命令参考进行分组),提供了丰富的,通用的接口,用于针对某种类型或某些键(通过KeyBound接口),如下表所述:

表1.操作视图
在这里插入图片描述

配置后,该模板是线程安全的,并且可以在多个实例之间重用。

RedisTemplate大多数操作都使用基于Java的序列化器。这意味着模板编写或读取的任何对象都将通过Java进行序列化和反序列化。您可以在模板上更改序列化机制,Redis模块提供了几种实现,可在org.springframework.data.redis.serializer软件包中找到。有关更多信息,请参见序列化器。您还可以将任何序列化器设置为null,并通过将enableDefaultSerializer属性设置为来将RedisTemplate与原始字节数组一起使用false。请注意,模板要求所有键都不为空。但是,只要基础串行器接受这些值,它们就可以为空。阅读每个序列化器的Javadoc,以获取更多信息。

通过RedisTemplate处理对象:

以字符串为中心的便利课程
由于存储在Redis中的键和值是很常见的java.lang.String,因此Redis模块提供了RedisConnection和的两个扩展RedisTemplate,分别是StringRedisConnection(及其DefaultStringRedisConnection实现),并且StringRedisTemplate是用于密集型String操作的便捷的一站式解决方案。除了绑定到String键外,模板和连接还使用StringRedisSerializer下方,这意味着所存储的键和值是人类可读的(假设Redis和您的代码中使用相同的编码)。

RedisTemplate 常用五大类型API:

RedisTemplate《-----------集成-------------stringRedisTemplate
stringRedisTemplate常用 API:
stringRedisTemplate.opsForValue();//操作字符串

1、set(K key, V value)
  新增一个字符串类型的值,key是键,value是值。
2、get(Object key)
  获取key键对应的值。 
3、append(K key, String value)
在原有的值基础上新增字符串到末尾。
4、get(K key, long start, long end)
截取key键对应值得字符串,从开始下标位置开始到结束下标的位置(包含结束下标)的字符串。
5、getAndSet(K key, V value)
  获取原来key键对应的值并重新赋新值。
6、setBit(K key, long offset, boolean value)
  key键对应的值value对应的ascii码,在offset的位置(从左向右数)变为value。
 7、getBit(K key, long offset)
  判断指定的位置ASCII码的bit位是否为1。
8、size(K key)
  获取指定字符串的长度
​​​​​​​9、increment(K key, double delta)
  以增量的方式将double值存储在变量中。
10、increment(K key, long delta)
  以增量的方式将long值存储在变量中。
11、setIfAbsent(K key, V value)
  如果键不存在则新增,存在则不改变已经有的值。
12、set(K key, V value, long timeout, TimeUnit unit)
  设置变量值的过期时间。
13、set(K key, V value, long offset)
  覆盖从指定位置开始的值。
14、multiSet(Map<? extends K,? extends V> map)
  设置map集合到redis。
15、multiGet(Collection<K> keys)
  根据集合取出对应的value值。
16、multiSetIfAbsent(Map<? extends K,? extends V> map)

stringRedisTemplate.opsForHash();//操作hash

1、put(H key, HK hashKey, HV value)
新增hashMap值。

redisTemplate.opsForHash().put("hashValue","map1","map1-1");  
redisTemplate.opsForHash().put("hashValue","map2","map2-2");  
 
2、values(H key)
获取指定变量中的hashMap值。

List<Object> hashList = redisTemplate.opsForHash().values("hashValue");  
System.out.println("通过values(H key)方法获取变量中的hashMap值:" + hashList);  
3、entries(H key)
获取变量中的键值对。

Map<Object,Object> map = redisTemplate.opsForHash().entries("hashValue");  
System.out.println("通过entries(H key)方法获取变量中的键值对:" + map);  
 
4、get(H key, Object hashKey)
 获取变量中的指定map键是否有值,如果存在该map键则获取值,没有则返回null。

Object mapValue = redisTemplate.opsForHash().get("hashValue","map1");  
System.out.println("通过get(H key, Object hashKey)方法获取map键的值:" + mapValue);  
 
5、hasKey(H key, Object hashKey)
 判断变量中是否有指定的map键。

boolean hashKeyBoolean = redisTemplate.opsForHash().hasKey("hashValue","map3");  
System.out.println("通过hasKey(H key, Object hashKey)方法判断变量中是否存在map键:" + hashKeyBoolean);  
 
6、keys(H key)
获取变量中的键。

Set<Object> keySet = redisTemplate.opsForHash().keys("hashValue");  
System.out.println("通过keys(H key)方法获取变量中的键:" + keySet);  
 
7、size(H key)
 获取变量的长度。

long hashLength = redisTemplate.opsForHash().size("hashValue");  
System.out.println("通过size(H key)方法获取变量的长度:" + hashLength);  
 
8、increment(H key, HK hashKey, double delta)
使变量中的键以double值的大小进行自增长。

double hashIncDouble = redisTemplate.opsForHash().increment("hashInc","map1",3);  
System.out.println("通过increment(H key, HK hashKey, double delta)方法使变量中的键以值的大小进行自增长:" + hashIncDouble); 
 
9、increment(H key, HK hashKey, long delta)
使变量中的键以long值的大小进行自增长。

long hashIncLong = redisTemplate.opsForHash().increment("hashInc","map2",6);  
System.out.println("通过increment(H key, HK hashKey, long delta)方法使变量中的键以值的大小进行自增长:" + hashIncLong);  
 
10、multiGet(H key, Collection<HK> hashKey)
 以集合的方式获取变量中的值。

List<Object> list = new ArrayList<Object>();  
list.add("map1");  
list.add("map2");  
List mapValueList = redisTemplate.opsForHash().multiGet("hashValue",list);  
System.out.println("通过multiGet(H key, Collection<HK> hashKeys)方法以集合的方式获取变量中的值:"+mapValueList);  
 
11、putAll(H key, Map<? extends HK,? extends HV> m)
以map集合的形式添加键值对。

Map newMap = new HashMap();  
newMap.put("map3","map3-3");  
newMap.put("map5","map5-5");  
redisTemplate.opsForHash().putAll("hashValue",newMap);  
map = redisTemplate.opsForHash().entries("hashValue");  
System.out.println("通过putAll(H key, Map<? extends HK,? extends HV> m)方法以map集合的形式添加键值对:" + map);  
 
12、putIfAbsent(H key, HK hashKey, HV value)
如果变量值存在,在变量中可以添加不存在的的键值对,如果变量不存在,则新增一个变量,同时将键值对添加到该变量。

redisTemplate.opsForHash().putIfAbsent("hashValue","map6","map6-6");  
map = redisTemplate.opsForHash().entries("hashValue");  
System.out.println("通过putIfAbsent(H key, HK hashKey, HV value)方法添加不存在于变量中的键值对:" + map);  
 
13、scan(H key, ScanOptions options)
匹配获取键值对,ScanOptions.NONE为获取全部键对,ScanOptions.scanOptions().match("map1").build()     匹配获取键位map1的键值对,不能模糊匹配。

Cursor<Map.Entry<Object,Object>> cursor = redisTemplate.opsForHash().scan("hashValue",ScanOptions.scanOptions().match("map1").build());  
//Cursor<Map.Entry<Object,Object>> cursor = redisTemplate.opsForHash().scan("hashValue",ScanOptions.NONE);  
while (cursor.hasNext()){  
    Map.Entry<Object,Object> entry = cursor.next();  
    System.out.println("通过scan(H key, ScanOptions options)方法获取匹配键值对:" + entry.getKey() + "---->" + entry.getValue());  
 
14、delete(H key, Object... hashKeys)
删除变量中的键值对,可以传入多个参数,删除多个键值对。

redisTemplate.opsForHash().delete("hashValue","map1","map2");  
map = redisTemplate.opsForHash().entries("hashValue");  
System.out.println("通过delete(H key, Object... hashKeys)方法删除变量中的键值对后剩余的:" + map);

rstringRedisTemplate.opsForList();//操作list

1、leftPush(K key, V value)

  在变量左边添加元素值。
Java代码  
redisTemplate.opsForList().leftPush("list","a");  
redisTemplate.opsForList().leftPush("list","b");  
redisTemplate.opsForList().leftPush("list","c");  
2、index(K key, long index)

  获取集合指定位置的值。
Java代码  
String listValue = redisTemplate.opsForList().index("list",1) + "";  
System.out.println("通过index(K key, long index)方法获取指定位置的值:" + listValue);  
     3、range(K key, long start, long end)
     
获取指定区间的值。
Java代码  
List<Object> list =  redisTemplate.opsForList().range("list",0,-1);  
System.out.println("通过range(K key, long start, long end)方法获取指定范围的集合值:"+list);  
 4、leftPush(K key, V pivot, V value)
 
     把最后一个参数值放到指定集合的第一个出现中间参数的前面,如果中间参数值存在的话。
Java代码  
redisTemplate.opsForList().leftPush("list","a","n");  
list =  redisTemplate.opsForList().range("list",0,-1);  
System.out.println("通过leftPush(K key, V pivot, V value)方法把值放到指定参数值前面:" + list);  
     5、leftPushAll(K key, V... values)
     
  向左边批量添加参数元素。
Java代码  
redisTemplate.opsForList().leftPushAll("list","w","x","y");  
list =  redisTemplate.opsForList().range("list",0,-1);  
System.out.println("通过leftPushAll(K key, V... values)方法批量添加元素:" + list);  
     6、leftPushAll(K key, Collection<V> values)
     
   以集合的方式向左边批量添加元素。
Java代码  
List newList = new ArrayList();  
newList.add("o");  
newList.add("p");  
newList.add("q");  
redisTemplate.opsForList().leftPushAll("list",newList);  
list =  redisTemplate.opsForList().range("list",0,-1);  
System.out.println("通过leftPushAll(K key, Collection<V> values)方法以集合的方式批量添加元素:" + list);  
     7、leftPushIfPresent(K key, V value)
     
  如果存在集合则添加元素。
Java代码  
redisTemplate.opsForList().leftPushIfPresent("presentList","o");  
list =  redisTemplate.opsForList().range("presentList",0,-1);  
System.out.println("通过leftPushIfPresent(K key, V value)方法向已存在的集合添加元素:" + list);  
     8、rightPush(K key, V value)
     
   向集合最右边添加元素。
Java代码  
redisTemplate.opsForList().rightPush("list","w");  
list =  redisTemplate.opsForList().range("list",0,-1);  
System.out.println("通过rightPush(K key, V value)方法向最右边添加元素:" + list);  
      9、rightPush(K key, V pivot, V value)
      
   向集合中第一次出现第二个参数变量元素的右边添加第三个参数变量的元素值。
Java代码  

redisTemplate.opsForList().rightPush("list","w","r");  
list =  redisTemplate.opsForList().range("list",0,-1);  
System.out.println("通过rightPush(K key, V pivot, V value)方法向最右边添加元素:" + list);  
     10、rightPushAll(K key, V... values)
     
   向右边批量添加元素。
Java代码  
redisTemplate.opsForList().rightPushAll("list","j","k");  
list =  redisTemplate.opsForList().range("list",0,-1);  
System.out.println("通过rightPushAll(K key, V... values)方法向最右边批量添加元素:" + list);  
    11、rightPushAll(K key, Collection<V> values)
    
   以集合方式向右边添加元素。
Java代码  
newList.clear();  
newList.add("g");  
newList.add("h");  
redisTemplate.opsForList().rightPushAll("list",newList);  
list =  redisTemplate.opsForList().range("list",0,-1);  
System.out.println("通过rightPushAll(K key, Collection<V> values)方法向最右边以集合方式批量添加元素:" + list);  
    12、rightPushIfPresent(K key, V value)
    
  向已存在的集合中添加元素。
Java代码  
redisTemplate.opsForList().rightPushIfPresent("presentList","d");  
list =  redisTemplate.opsForList().range("presentList",0,-1);  
System.out.println("通过rightPushIfPresent(K key, V value)方法已存在的集合向最右边添加元素:" + list);  
    13、size(K key)
    
获取集合长度。
Java代码  
long listLength = redisTemplate.opsForList().size("list");  
System.out.println("通过size(K key)方法获取集合list的长度为:" + listLength);  
     14、leftPop(K key)
     
       移除集合中的左边第一个元素。
Java代码  
Object popValue = redisTemplate.opsForList().leftPop("list");  
System.out.print("通过leftPop(K key)方法移除的元素是:" + popValue);  
list =  redisTemplate.opsForList().range("list",0,-1);  
System.out.println(",剩余的元素是:" + list);  
     15、leftPop(K key, long timeout, TimeUnit unit)
     
   移除集合中左边的元素在等待的时间里,如果超过等待的时间仍没有元素则退出。
Java代码  
popValue = redisTemplate.opsForList().leftPop("presentList",1, TimeUnit.SECONDS);  
System.out.print("通过leftPop(K key, long timeout, TimeUnit unit)方法移除的元素是:" + popValue);  
list =  redisTemplate.opsForList().range("presentList",0,-1);  
System.out.println(",剩余的元素是:" + list);  
     16、rightPop(K key)
     
        移除集合中右边的元素。
Java代码  
popValue = redisTemplate.opsForList().rightPop("list");  
System.out.print("通过rightPop(K key)方法移除的元素是:" + popValue);  
list =  redisTemplate.opsForList().range("list",0,-1);  
System.out.println(",剩余的元素是:" + list);  
      17、rightPop(K key, long timeout, TimeUnit unit)
      
   移除集合中右边的元素在等待的时间里,如果超过等待的时间仍没有元素则退出。
Java代码  
popValue = redisTemplate.opsForList().rightPop("presentList",1, TimeUnit.SECONDS);  
System.out.print("通过rightPop(K key, long timeout, TimeUnit unit)方法移除的元素是:" + popValue);  
list =  redisTemplate.opsForList().range("presentList",0,-1);  
System.out.println(",剩余的元素是:" + list);  
     18、rightPopAndLeftPush(K sourceKey, K destinationKey)
     
  移除集合中右边的元素,同时在左边加入一个元素。
Java代码  
popValue = redisTemplate.opsForList().rightPopAndLeftPush("list","12");  
System.out.print("通过rightPopAndLeftPush(K sourceKey, K destinationKey)方法移除的元素是:" + popValue);  
list =  redisTemplate.opsForList().range("list",0,-1);  
System.out.println(",剩余的元素是:" + list);  
     19、rightPopAndLeftPush(K sourceKey, K destinationKey, long timeout, TimeUnit unit)
     
  移除集合中右边的元素在等待的时间里,同时在左边添加元素,如果超过等待的时间仍没有元素则退出。
Java代码  
popValue = redisTemplate.opsForList().rightPopAndLeftPush("presentList","13",1,TimeUnit.SECONDS);  
System.out.println("通过rightPopAndLeftPush(K sourceKey, K destinationKey, long timeout, TimeUnit unit)方法移除的元素是:" + popValue);  
list =  redisTemplate.opsForList().range("presentList",0,-1);  
System.out.print(",剩余的元素是:" + list);  
  20、set(K key, long index, V value)
  
  在集合的指定位置插入元素,如果指定位置已有元素,则覆盖,没有则新增,超过集合下标+n则会报错。
Java代码  
redisTemplate.opsForList().set("presentList",3,"15");  
list =  redisTemplate.opsForList().range("presentList",0,-1);  
System.out.print("通过set(K key, long index, V value)方法在指定位置添加元素后:" + list);  
  21、remove(K key, long count, Object value)
  
  从存储在键中的列表中删除等于值的元素的第一个计数事件。count> 0:删除等于从左到右移动的值的第一个元素;count< 0:删除等于从右到左移动的值的第一个元素;count = 0:删除等于value的所有元素。
Java代码  
long removeCount = redisTemplate.opsForList().remove("list",0,"w");  
list =  redisTemplate.opsForList().range("list",0,-1);  
System.out.println("通过remove(K key, long count, Object value)方法移除元素数量:" + removeCount);  
System.out.println(",剩余的元素:" + list);  
      22、trim(K key, long start, long end)
      
  截取集合元素长度,保留长度内的数据。
Java代码  
redisTemplate.opsForList().trim("list",0,5);  
list =  redisTemplate.opsForList().range("list",0,-1);  
System.out.println("通过trim(K key, long start, long end)方法截取后剩余元素:" + list);  

stringRedisTemplate.opsForSet();//操作set

  1、add(K key, V... values)
  向变量中批量添加值。
redisTemplate.opsForSet().add("setValue","A","B","C","B","D","E","F");  
 

  2、members(K key)
  获取变量中的值。
Set set = redisTemplate.opsForSet().members("setValue");  
System.out.println("通过members(K key)方法获取变量中的元素值:" + set);  
 

  3、size(K key)
   获取变量中值的长度。
long setLength = redisTemplate.opsForSet().size("setValue");  
System.out.println("通过size(K key)方法获取变量中元素值的长度:" + setLength);  
 

  4、randomMember(K key)
   随机获取变量中的元素。
Object randomMember = redisTemplate.opsForSet().randomMember("setValue");  
System.out.println("通过randomMember(K key)方法随机获取变量中的元素:" + randomMember);  
 

  5、randomMembers(K key, long count)
  随机获取变量中指定个数的元素。
List randomMembers = redisTemplate.opsForSet().randomMembers("setValue",2);  
System.out.println("通过randomMembers(K key, long count)方法随机获取变量中指定个数的元素:" + randomMembers);  
 

  6、isMember(K key, Object o)
  检查给定的元素是否在变量中。
boolean isMember = redisTemplate.opsForSet().isMember("setValue","A");  
System.out.println("通过isMember(K key, Object o)方法检查给定的元素是否在变量中:" + isMember);  
 

  7、move(K key, V value, K destKey)
   转移变量的元素值到目的变量。
boolean isMove = redisTemplate.opsForSet().move("setValue","A","destSetValue");  
if(isMove){  
    set = redisTemplate.opsForSet().members("setValue");  
    System.out.print("通过move(K key, V value, K destKey)方法转移变量的元素值到目的变量后的剩余元素:" + set);  
    set = redisTemplate.opsForSet().members("destSetValue");  
    System.out.println(",目的变量中的元素值:" + set);  
}  
 

  8、pop(K key)
   弹出变量中的元素。
Object popValue = redisTemplate.opsForSet().pop("setValue");  
System.out.print("通过pop(K key)方法弹出变量中的元素:" + popValue);  
set = redisTemplate.opsForSet().members("setValue");  
System.out.println(",剩余元素:" + set)  


  9、remove(K key, Object... values)
   批量移除变量中的元素。
long removeCount = redisTemplate.opsForSet().remove("setValue","E","F","G");  
System.out.print("通过remove(K key, Object... values)方法移除变量中的元素个数:" + removeCount);  
set = redisTemplate.opsForSet().members("setValue");  
System.out.println(",剩余元素:" + set);

  
  10、scan(K key, ScanOptions options)
   匹配获取键值对,ScanOptions.NONE为获取全部键值对;ScanOptions.scanOptions().match("C").build()匹配获取键位map1的键值对,不能模糊匹配。
//Cursor<Object> cursor = redisTemplate.opsForSet().scan("setValue", ScanOptions.NONE);  
Cursor<Object> cursor = redisTemplate.opsForSet().scan("setValue", ScanOptions.scanOptions().match("C").build());  
while (cursor.hasNext()){  
    Object object = cursor.next();  
    System.out.println("通过scan(K key, ScanOptions options)方法获取匹配的值:" + object);  
}  


  11、difference(K key, Collection<K> otherKeys)
   通过集合求差值。
List list = new ArrayList();  
list.add("destSetValue");  
Set differenceSet = redisTemplate.opsForSet().difference("setValue",list);  
System.out.println("通过difference(K key, Collection<K> otherKeys)方法获取变量中与给定集合中变量不一样的值:" + differenceSet);  
        
        
  12、difference(K key, K otherKey)
   通过给定的key求2个set变量的差值。
differenceSet = redisTemplate.opsForSet().difference("setValue","destSetValue");  
System.out.println("通过difference(K key, Collection<K> otherKeys)方法获取变量中与给定变量不一样的值:" + differenceSet);  


  13、differenceAndStore(K key, K otherKey, K destKey)
    将求出来的差值元素保存。
redisTemplate.opsForSet().differenceAndStore("setValue","destSetValue","storeSetValue");  
set = redisTemplate.opsForSet().members("storeSetValue");  
System.out.println("通过differenceAndStore(K key, K otherKey, K destKey)方法将求出来的差值元素保存:" + set); 

 
  14、differenceAndStore(K key, Collection<K> otherKeys, K destKey)
    将求出来的差值元素保存。
redisTemplate.opsForSet().differenceAndStore("setValue",list,"storeSetValue");  
set = redisTemplate.opsForSet().members("storeSetValue");  
System.out.println("通过differenceAndStore(K key, Collection<K> otherKeys, K destKey)方法将求出来的差值元素保存:" + set); 

 
  15、distinctRandomMembers(K key, long count)
     获取去重的随机元素。
set = redisTemplate.opsForSet().distinctRandomMembers("setValue",2);  
System.out.println("通过distinctRandomMembers(K key, long count)方法获取去重的随机元素:" + set);  


  16、intersect(K key, K otherKey)
    获取2个变量中的交集。
set = redisTemplate.opsForSet().intersect("setValue","destSetValue");  
System.out.println("通过intersect(K key, K otherKey)方法获取交集元素:" + set); 

 
  17、intersect(K key, Collection<K> otherKeys) 
    获取多个变量之间的交集。
set = redisTemplate.opsForSet().intersect("setValue",list);  
System.out.println("通过intersect(K key, Collection<K> otherKeys)方法获取交集元素:" + set); 

 
  18、intersectAndStore(K key, K otherKey, K destKey)
     获取2个变量交集后保存到最后一个参数上。
redisTemplate.opsForSet().intersectAndStore("setValue","destSetValue","intersectValue");  
set = redisTemplate.opsForSet().members("intersectValue");  
System.out.println("通过intersectAndStore(K key, K otherKey, K destKey)方法将求出来的交集元素保存:" + set);  


  19、intersectAndStore(K key, Collection<K> otherKeys, K destKey)  
     获取多个变量的交集并保存到最后一个参数上。
redisTemplate.opsForSet().intersectAndStore("setValue",list,"intersectListValue");  
set = redisTemplate.opsForSet().members("intersectListValue");  
System.out.println("通过intersectAndStore(K key, Collection<K> otherKeys, K destKey)方法将求出来的交集元素保存:" + set); 

 
  20、union(K key, K otherKey)
     获取2个变量的合集。
set = redisTemplate.opsForSet().union("setValue","destSetValue");  
System.out.println("通过union(K key, K otherKey)方法获取2个变量的合集元素:" + set);

  
  21、union(K key, Collection<K> otherKeys)
    获取多个变量的合集。
set = redisTemplate.opsForSet().union("setValue",list);  
System.out.println("通过union(K key, Collection<K> otherKeys)方法获取多个变量的合集元素:" + set);  


  22、unionAndStore(K key, K otherKey, K destKey)
    获取2个变量合集后保存到最后一个参数上。
redisTemplate.opsForSet().unionAndStore("setValue","destSetValue","unionValue");  
set = redisTemplate.opsForSet().members("unionValue");  
System.out.println("通过unionAndStore(K key, K otherKey, K destKey)方法将求出来的交集元素保存:" + set);

  
  23、unionAndStore(K key, Collection<K> otherKeys, K destKey)     
    获取多个变量的合集并保存到最后一个参数上。
redisTemplate.opsForSet().unionAndStore("setValue",list,"unionListValue");  
set = redisTemplate.opsForSet().members("unionListValue");  
System.out.println("通过unionAndStore(K key, Collection<K> otherKeys, K destKey)方法将求出来的交集元素保存:" + set);

stringRedisTemplate.opsForZSet();//操作有序set

	1、add(K key, V value, double score)
	添加元素到变量中同时指定元素的分值。
	redisTemplate.opsForZSet().add("zSetValue","A",1);  
	redisTemplate.opsForZSet().add("zSetValue","B",3);  
	redisTemplate.opsForZSet().add("zSetValue","C",2);  
	redisTemplate.opsForZSet().add("zSetValue","D",5);  
	 
	 
	2、range(K key, long start, long end)
	获取变量指定区间的元素。
	Set zSetValue = redisTemplate.opsForZSet().range("zSetValue",0,-1);  
	System.out.println("指定区间的元素:" + zSetValue);  
	 
	 
	3、rangeByLex(K key, RedisZSetCommands.Range range)
	用于获取满足非score的排序取值。这个排序只有在有相同分数的情况下才能使用,如果有不同的分数则返回值不确定。
	RedisZSetCommands.Range range = new RedisZSetCommands.Range();  
	//range.gt("A");  
	range.lt("D");  
	zSetValue = redisTemplate.opsForZSet().rangeByLex("zSetValue", range);  
	System.out.println("获取满足非score的排序取值元素:" + zSetValue);  
	           
	 
	4、rangeByLex(K key, RedisZSetCommands.Range range, RedisZSetCommands.Limit limit)
	用于获取满足非score的设置下标开始的长度排序取值。
	RedisZSetCommands.Limit limit = new RedisZSetCommands.Limit();  
	limit.count(2);  
	//起始下标为0  
	limit.offset(1);  
	zSetValue = redisTemplate.opsForZSet().rangeByLex("zSetValue", range,limit);  
	System.out.println("满足非score的排序取值元素:" + zSetValue);  
	 
	 
	5、add(K key, Set<ZSetOperations.TypedTuple<V>> tuples)
	通过TypedTuple方式新增数据。
	ZSetOperations.TypedTuple<String> typedTuple1 = new DefaultTypedTuple<String>("E",6.0);  
	ZSetOperations.TypedTuple<String> typedTuple2 = new DefaultTypedTuple<String>("F",7.0);  
	ZSetOperations.TypedTuple<String> typedTuple3 = new DefaultTypedTuple<String>("G",5.0);  
	Set<ZSetOperations.TypedTuple<String>> typedTupleSet = new HashSet<ZSetOperations.TypedTuple<String>>();  
	typedTupleSet.add(typedTuple1);  
	typedTupleSet.add(typedTuple2);  
	typedTupleSet.add(typedTuple3);  
	redisTemplate.opsForZSet().add("typedTupleSet",typedTupleSet);  
	zSetValue = redisTemplate.opsForZSet().range("typedTupleSet",0,-1);  
	System.out.println("添加元素:" + zSetValue);  
	 
	 
	6、rangeByScore(K key, double min, double max)
	根据设置的score获取区间值。
	zSetValue = redisTemplate.opsForZSet().rangeByScore("zSetValue",1,2);  
	System.out.println("区间值:" + zSetValue);  
	 
	 
	7、rangeByScore(K key, double min, double max,long offset, long count)
	根据设置的score获取区间值从给定下标和给定长度获取最终值。
	zSetValue = redisTemplate.opsForZSet().rangeByScore("zSetValue",1,5,1,3);  
	System.out.println("根据设置的score获取区间值:" + zSetValue);  
	 
	 
	8、rangeWithScores(K key, long start, long end)
	获取RedisZSetCommands.Tuples的区间值。
	Set<ZSetOperations.TypedTuple<String>> typedTupleSet = redisTemplate.opsForZSet().rangeWithScores("typedTupleSet",1,3);  
	Iterator<ZSetOperations.TypedTuple<String>> iterator = typedTupleSet.iterator();  
	while (iterator.hasNext()){  
	    ZSetOperations.TypedTuple<String> typedTuple = iterator.next();  
	    Object value = typedTuple.getValue();  
	    double score = typedTuple.getScore();  
	    System.out.println("获取RedisZSetCommands.Tuples的区间值:" + value + "---->" + score );  
	}  
	 
	 
	9、rangeByScoreWithScores(K key, double min, double max)
	获取RedisZSetCommands.Tuples的区间值通过分值。
	Set<ZSetOperations.TypedTuple<String>> typedTupleSet = redisTemplate.opsForZSet().rangeByScoreWithScores("typedTupleSet",5,8);  
	iterator = typedTupleSet.iterator();  
	while (iterator.hasNext()){  
	    ZSetOperations.TypedTuple<String> typedTuple = iterator.next();  
	    Object value = typedTuple.getValue();  
	    double score = typedTuple.getScore();  
	    System.out.println("区间值通过分值:" + value + "---->" + score );  
	}  
	 
	 
	10、rangeByScoreWithScores(K key, double min, double max, long offset, long count)
	获取RedisZSetCommands.Tuples的区间值从给定下标和给定长度获取最终值通过分值。
	Set<ZSetOperations.TypedTuple<String>> typedTupleSet = redisTemplate.opsForZSet().rangeByScoreWithScores("typedTupleSet",5,8,1,1);  
	iterator = typedTupleSet.iterator();  
	while (iterator.hasNext()){  
	    ZSetOperations.TypedTuple<String> typedTuple = iterator.next();  
	    Object value = typedTuple.getValue();  
	    double score = typedTuple.getScore();  
	    System.out.println("通过分值:" + value + "---->" + score );  
	}  
	 
	 
	11、count(K key, double min, double max)
	获取区间值的个数。
	long count = redisTemplate.opsForZSet().count("zSetValue",1,5);  
	System.out.println("个数:" + count);  
	 
	 
	12、rank(K key, Object o)
	获取变量中元素的索引,下标开始位置为0。
	long index = redisTemplate.opsForZSet().rank("zSetValue","B");  
	System.out.println("获取变量中元素的索引:" + index);  
	 
	 
	13、scan(K key, ScanOptions options)
	匹配获取键值对,ScanOptions.NONE为获取全部键值对;ScanOptions.scanOptions().match("C").build()匹配获取键位map1的键值对,不能模糊匹配。
	//Cursor<String> cursor = redisTemplate.opsForSet().scan("setValue", ScanOptions.NONE);  
	Cursor<ZSetOperations.TypedTuple<String>> cursor = redisTemplate.opsForZSet().scan("zSetValue", ScanOptions.NONE);  
	while (cursor.hasNext()){  
	    ZSetOperations.TypedTuple<String> typedTuple = cursor.next();  
	    System.out.println("元素:" + typedTuple.getValue() + "--->" + typedTuple.getScore());  
	}  
	 
	 
	14、score(K key, Object o)
	获取元素的分值。
	double score = redisTemplate.opsForZSet().score("zSetValue","B");  
	System.out.println("元素的分值:" + score);  
	 
	 
	15、zCard(K key)
	获取变量中元素的个数。
	long zCard = redisTemplate.opsForZSet().zCard("zSetValue");  
	System.out.println("变量的长度:" + zCard);  
	 
	 
	16、incrementScore(K key, V value, double delta)
	修改变量中的元素的分值。
	double incrementScore = redisTemplate.opsForZSet().incrementScore("zSetValue","C",5);  
	System.out.print("通过incrementScore(K key, V value, double delta)方法修改变量中的元素的分值:" + incrementScore);  
	score = redisTemplate.opsForZSet().score("zSetValue","C");  
	System.out.print(",修改后获取元素的分值:" + score);  
	zSetValue = redisTemplate.opsForZSet().range("zSetValue",0,-1);  
	System.out.println(",修改后排序的元素:" + zSetValue);  
	 
	 
	17、reverseRange(K key, long start, long end)
	索引倒序排列指定区间元素。
	zSetValue = redisTemplate.opsForZSet().reverseRange("zSetValue",0,-1);  
	System.out.println("倒序排列元素:" + zSetValue);  
	 
	 
	18、reverseRangeByScore(K key, double min, double max)
	倒序排列指定分值区间元素。
	zSetValue = redisTemplate.opsForZSet().reverseRangeByScore("zSetValue",1,5);  
	System.out.println("元素:" + zSetValue);  
	 
	 
	19、reverseRangeByScore(K key, double min, double max, long offset, long count)
	倒序排列从给定下标和给定长度分值区间元素。
	zSetValue = redisTemplate.opsForZSet().reverseRangeByScore("zSetValue",1,5,1,2);  
	System.out.println("区间元素:" + zSetValue);  
	 
	 
	20、reverseRangeByScoreWithScores(K key, double min, double max)
	倒序排序获取RedisZSetCommands.Tuples的分值区间值。
	Set<ZSetOperations.TypedTuple<String>> typedTupleSet = redisTemplate.opsForZSet().reverseRangeByScoreWithScores("zSetValue",1,5);  
	iterator = typedTupleSet.iterator();  
	while (iterator.hasNext()){  
	    ZSetOperations.TypedTuple<String> typedTuple = iterator.next();  
	    Object value = typedTuple.getValue();  
	    double score1 = typedTuple.getScore();  
	    System.out.println("区间值:" + value + "---->" + score1 );  
	}  
	 
	 
	21、reverseRangeByScoreWithScores(K key, double min, double max, long offset, long count)
	倒序排序获取RedisZSetCommands.Tuples的从给定下标和给定长度分值区间值。
	Set<ZSetOperations.TypedTuple<String>> typedTupleSet = redisTemplate.opsForZSet().reverseRangeByScoreWithScores("zSetValue",1,5,1,2);  
	iterator = typedTupleSet.iterator();  
	while (iterator.hasNext()){  
	    ZSetOperations.TypedTuple<String> typedTuple = iterator.next();  
	    Object value = typedTuple.getValue();  
	    double score1 = typedTuple.getScore();  
	    System.out.println("区间值:" + value + "---->" + score1 );  
	}  
	 
	 
	22、reverseRangeWithScores(K key, long start, long end)
	索引倒序排列区间值。
	Set<ZSetOperations.TypedTuple<String>> typedTupleSet = redisTemplate.opsForZSet().reverseRangeWithScores("zSetValue",1,5);  
	iterator = typedTupleSet.iterator();  
	while (iterator.hasNext()){  
	    ZSetOperations.TypedTuple<String> typedTuple = iterator.next();  
	    Object value = typedTuple.getValue();  
	    double score1 = typedTuple.getScore();  
	    System.out.println("区间值:" + value + "----->" + score1);  
	}  
	 
	 
	23、reverseRank(K key, Object o)
	获取倒序排列的索引值。
	long reverseRank = redisTemplate.opsForZSet().reverseRank("zSetValue","B");  
	System.out.println("索引值:" + reverseRank);  
	 
	 
	24、intersectAndStore(K key, K otherKey, K destKey)
	获取2个变量的交集存放到第3个变量里面。
	redisTemplate.opsForZSet().intersectAndStore("zSetValue","typedTupleSet","intersectSet");  
	zSetValue = redisTemplate.opsForZSet().range("intersectSet",0,-1);  
	System.out.println("里面:" + zSetValue); 
	 
	 
	25、intersectAndStore(K key, Collection<K> otherKeys, K destKey)
	获取多个变量的交集存放到第3个变量里面。
	List list = new ArrayList();  
	list.add("typedTupleSet");  
	redisTemplate.opsForZSet().intersectAndStore("zSetValue",list,"intersectListSet");  
	zSetValue = redisTemplate.opsForZSet().range("intersectListSet",0,-1);  
	System.out.println("里面:" + zSetValue);
	 
	  
	26、unionAndStore(K key, K otherKey, K destKey)
	获取2个变量的合集存放到第3个变量里面。
	redisTemplate.opsForZSet().unionAndStore("zSetValue","typedTupleSet","unionSet");  
	zSetValue = redisTemplate.opsForZSet().range("unionSet",0,-1);  
	System.out.println("里面:" + zSetValue);  
	 
	 
	27、unionAndStore(K key, Collection<K> otherKeys, K destKey)
	获取多个变量的合集存放到第3个变量里面。
	redisTemplate.opsForZSet().unionAndStore("zSetValue",list,"unionListSet");  
	zSetValue = redisTemplate.opsForZSet().range("unionListSet",0,-1);  
	System.out.println("里面:" + zSetValue); 
	 
	 
	28、remove(K key, Object... values)
	批量移除元素根据元素值。
	long removeCount = redisTemplate.opsForZSet().remove("unionListSet","A","B");  
	zSetValue = redisTemplate.opsForZSet().range("unionListSet",0,-1);  
	System.out.print("移除元素的个数:" + removeCount);  
	System.out.println(",移除后剩余的元素:" + zSetValue);  
	 
	 
	29、removeRangeByScore(K key, double min, double max)
	根据分值移除区间元素。
	removeCount = redisTemplate.opsForZSet().removeRangeByScore("unionListSet",3,5);  
	zSetValue = redisTemplate.opsForZSet().range("unionListSet",0,-1);  
	System.out.print("移除元素的个数:" + removeCount);  
	System.out.println(",移除后剩余的元素:" + zSetValue);  
	 
	 
	30、removeRange(K key, long start, long end)
	根据索引值移除区间元素。
	removeCount = redisTemplate.opsForZSet().removeRange("unionListSet",3,5);  
	zSetValue = redisTemplate.opsForZSet().range("unionListSet",0,-1);  
	System.out.print("移除元素的个数:" + removeCount);  
	System.out.println(",移除后剩余的元素:" + zSetValue); 

Redis连接配置文件属性(springboot版本1.5之前的)

配置缓存redis

spring.redis.database=8

Redis服务器地址

spring.redis.host=127.0.0.1

Redis服务器连接端口

spring.redis.port=6379

Redis服务器连接密码(默认为空)

spring.redis.password=

连接池最大连接数(使用负值表示没有限制)

spring.redis.pool.max-active=8

连接池最大阻塞等待时间(使用负值表示没有限制)

spring.redis.pool.max-wait=-1

连接池中的最大空闲连接

spring.redis.pool.max-idle=8

连接池中的最小空闲连接

spring.redis.pool.min-idle=0

连接超时时间(毫秒)

spring.redis.keytimeout=1000
spring.redis.timeout=0

版本2:(springboot版本2.x)

spring:
  redis:
    host: 127.0.0.1
    password: 123456
    port: 6379
    jedis:
      pool:
        max-active: 1000
        max-wait: -1
        max-idle: 100
        min-idle: 1

springboot中常用的注解:

@Cacheable是想缓存中添加数据,
@CachePut 更新缓存中的数据,
@@CacheEvict是删除缓存中的数据

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

知青先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值