微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

redis的客户端命令和redis template的java命令的对照表

目录

Redis 数据类型

Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合)。

一、Redis 字符串(String)

ValueOperations valueOperations = redistemplate.opsForValue();
 
/**Set void*/
valueOperations.set("keyDemo","hello");
/**Set void 同一个key,会对原有的value进行复写,该set可以设置失效时间*/
 
valueOperations.set("keyDemo","hello word",30, TimeUnit.SECONDS);
 
/**Set void(复写) 从上一个值的6下标开始复写*/
valueOperations.set("keyDemo","redis",6);
//结果: hello redis
 
/**如果key已经存在,则该值追加到字符串的末尾。
 * 如果键不存在,则它被创建并设置为空字符串*/
valueOperations.append("keyDemo"," By");
valueOperations.append("keyDemo1","Hello");
// keyDemo  结果: hello redis By
// keyDemo1 结果: Hello
 
/**size Long 返回key所对应的value的长度*/
System.out.println("****5key****:"+valueOperations.size("keyDemo"));
// 结果:14
 
/**getAndSet String 先取Key对应的值,再进行赋值*/
System.out.println("****6key****:"+valueOperations.getAndSet("keyDemo","String Type is End!"));
// 结果 hello redis By
System.out.println("****7key****:"+valueOperations.get("keyDemo"));
// 结果 String Type is End!

二、Redis 哈希(Hash)

/**设置过期时间*/
redistemplate.expire("redisHash",10, TimeUnit.SECONDS);
HashOperations hashOperations = redistemplate.opsForHash();
 
 /**void put(H key, HK hashKey, HV value);
  设置散列hashKey的值*/
 hashOperations.put("redisHash","name","666");
 hashOperations.put("redisHash","age",26);
 hashOperations.put("redisHash","class","6");
 //结果:结果:{age=26, class=6, name=666}
 
 /**void putAll(H key, Map<? extends HK, ? extends HV> m);
  使用m中提供的多个散列字段设置到key对应的散列表中*/
 Map<String,Object> testMap = new HashMap();
 testMap.put("name","666");
 testMap.put("age",27);
 testMap.put("class","1");
 hashOperations.putAll("redisHash1",testMap);
 System.out.println(hashOperations.entries("redisHash1"));
 //结果:结果:{class=1, name=jack, age=27}
 
 /**Set<HK> keys(H key);
  获取key所对应的散列表的key*/
 System.out.println(hashOperations.keys("redisHash"));
 //结果:redisHash所对应的散列表为{class=1, name=666, age=27}
 //结果:结果:[name, class, age]
 
 /**
  * HV get(H key, Object hashKey);
  */
 System.out.println(hashOperations.get("redisHash","age"));
 //结果:结果:26
 
/**
 * Boolean hasKey(H key, Object hashKey);
 * 确定哈希hashKey是否存在
 */
 System.out.println(hashOperations.hasKey("redisHash","666")); //结果:true
 System.out.println(hashOperations.hasKey("redisHash","777"));//结果:false
 
 /**List<HV> values(H key);
  获取整个哈希存储的值根据密钥*/
 System.out.println(hashOperations.values("redisHash"));
 //结果:[666, 26, 6]
 
 /**Map<HK, HV> entries(H key);
  获取整个哈希存储根据密钥*/
 System.out.println(hashOperations.entries("redisHash"));
 //结果:{age=26, class=6, name=666}
 
 /** Cursor<Map.Entry<HK, HV>> scan(H key, Scanoptions options);
  使用Cursor在key的hash中迭代,相当于迭代器。*/
 Cursor<Map.Entry<Object, Object>> curosr = hashOperations.scan("redisHash",
         Scanoptions.NONE);
 while(curosr.hasNext()){
     Map.Entry<Object, Object> entry = curosr.next();
     System.out.println(entry.getKey()+":"+entry.getValue());
 }
 //结果:age:27  class:6  name:666
 
 /**
  * Long delete(H key, Object... hashKeys);
  * 删除给定的哈希hashKeys
  */
 System.out.println(hashOperations.delete("redisHash","name"));
 System.out.println(hashOperations.entries("redisHash"));
 //结果:1
 //结果:{class=6, age=26}

三、Redis 列表(List)

/**设置过期时间*/
redistemplate.expire("redisHash",10, TimeUnit.SECONDS);
 
ListOperations listOperations = redistemplate.opsForList();
/**leftPush Long
 * 将指定的值插入存储在键的列表的头部。(从左边插入)*/
Long aLong = listOperations.leftPush("listKey","kyee"); // 返回1
Long aLong1 = listOperations.leftPush("listKey","shiNow");//返回2
Long aLong2 = listOperations.leftPushAll("listKey", "aa", "bb", "cc");
 
/** leftPushAll(K key, V... values)  Long;
 批量把一批参数插入到列表中
 leftPushAll(Collection collection)
 把一个集合插入List表
 */
String[] str = {"1","2","3"};
List<String> strings = Arrays.asList(str);
Long aLong3 = listOperations.leftPushAll("listKey", "aa", "bb", "cc");
Long aLong4 = listOperations.leftPushAll("listKey", strings);
 
/**leftPush && leftPushAll 两个命令和上述类似,不做介绍*/
/** set(K key, long index, V value) 在列表中index的位置设置并覆盖value值*/
listOperations.set("listKey",2,"test");
//结果:kyee,shiNow,test,bb,cc, .....
 
/**Long remove(K key, long count, Object value);
 * count<0 删除从尾到头的第一个等于value的元素
 * count=0 删除所有等于value的元素
 * count>0 删除从头到尾第一个等于value的元素
 * */
Long remove0 = listOperations.remove("listKey", 0, "aa");
Long remove1 = listOperations.remove("listKey", 1, "aa");
Long remove2 = listOperations.remove("listKey", -1, "aa");
 
/**Object index(K key, long index);
 * 根据下表获取列表中的值*/
listOperations.index("listKey",2);
 
/**Object leftPop(K key);
 弹出最左边的元素,弹出之后该值在列表中将不复存在*/
listOperations.leftPop("listKey");
 
/**
 * leftPop(K key, long timeout, TimeUnit unit)
 *移除集合中左边的元素在等待的时间里,如果超过等待的时间仍没有元素则退出。*/
listOperations.leftPop("listKey",10,TimeUnit.SECONDS);
/**rightPop  两个命令和上述类似,不做介绍*/

四、Redis 集合(Set)

Setoperations setoperations = redistemplate.opsForSet();
/**Long add(K key, V... values);
 无序集合中添加元素,返回添加个数
 也可以直接在add里面添加多个值 如:template.opsForSet().add("setTest","aaa","bbb")*/
String[] strs= new String[]{"str1","str2"};
System.out.println(setoperations.add("setTest", strs));
//结果:2
 
/**Long remove(K key, Object... values);
 移除集合中一个或多个成员*/
System.out.println(setoperations.remove("setTest",strs));
//结果:2
 
/**V pop(K key);
 移除并返回集合中的一个随机元素*/
System.out.println(setoperations.pop("setTest"));
System.out.println(setoperations.members("setTest"));
//结果:bbb
//结果: [aaa, ccc]
 
/**Boolean move(K key, V value, K destKey);
 将 member 元素从 source 集合移动到 destination 集合*/
setoperations.move("setTest","aaa","setTest2");
System.out.println(setoperations.members("setTest"));
System.out.println(setoperations.members("setTest2"));
//结果:[ccc]
//结果:[aaa]
 
/**Cursor<V> scan(K key, Scanoptions options);
 遍历set*/
Cursor<Object> curosr = setoperations.scan("setTest", Scanoptions.NONE);
while(curosr.hasNext()){
    System.out.println(curosr.next());
}

五、Redis 有序集合(sorted set)

ZSetoperations zSetoperations = redistemplate.opsForZSet();
/**
 *  Boolean add(K key, V value, double score);
 * 新增一个有序集合,存在的话为false,不存在的话为true
 * ZSet内部是按照score排序
 * */
zSetoperations.add("Z-set1","abc",2);
 
/**Long add(K key, Set<TypedTuple<V>> tuples);*/
ZSetoperations.TypedTuple<Object> objectTypedTuple1 = new DefaultTypedTuple<>("zset-5",9.6);
ZSetoperations.TypedTuple<Object> objectTypedTuple2 = new DefaultTypedTuple<>("zset-6",9.9);
Set<ZSetoperations.TypedTuple<Object>> tuples = new HashSet<ZSetoperations.TypedTuple<Object>>();
tuples.add(objectTypedTuple1);
tuples.add(objectTypedTuple2);
System.out.println(zSetoperations.add("zset1",tuples));
System.out.println(zSetoperations.range("zset1",0,-1));
//结果:[zset-1, zset-2, zset-3, zset-4, zset-5, zset-6]
 
/**Long size(K key);
 获取有序集合的成员数,内部调用的就是zCard方法*/
System.out.println(zSetoperations.size("zset1"));
//结果:6
 
/**Long remove(K key, Object... values);
 从有序集合中移除一个或者多个元素*/
System.out.println(zSetoperations.range("zset1",0,-1));
System.out.println(zSetoperations.remove("zset1","zset-6"));
System.out.println(zSetoperations.range("zset1",0,-1));
//结果:[zset-1, zset-2, zset-3, zset-4, zset-5, zset-6]
//结果: 1
//结果:[zset-1, zset-2, zset-3, zset-4, zset-5]
 
/**Long rank(K key, Object o);
 返回有序集中指定成员的排名,其中有序集成员按分数值递增(从小到大)顺序排列*/
System.out.println(zSetoperations.range("zset1",0,-1));
System.out.println(zSetoperations.rank("zset1","zset-2"));
//结果:[zset-2, zset-1, zset-3, zset-4, zset-5]
//结果:0   表明排名第一
 
/**Cursor<TypedTuple<V>> scan(K key, Scanoptions options);
 遍历zset*/
Cursor<ZSetoperations.TypedTuple<Object>> cursor = zSetoperations.scan("zzset1", Scanoptions.NONE);
while (cursor.hasNext()){
    ZSetoperations.TypedTuple<Object> item = cursor.next();
    System.out.println(item.getValue() + ":" + item.getscore());
}
//结果:zset-1:1.0
//结果:zset-2:2.0
//结果:zset-3:3.0
//结果:zset-4:6.0
 
/**Long count(K key, double min, double max);
 通过分数返回有序集合指定区间内的成员个数*/
System.out.println(zSetoperations.rangeByscore("zset1",0,5));
System.out.println(zSetoperations.count("zset1",0,5));
//结果:[zset-2, zset-1, zset-3]
//结果: 3
 
/**Double score(K key, Object o);
 获取指定成员的score值*/
System.out.println(zSetoperations.score("zset1","zset-1"));
//结果:2.2
 
/**Long removeRange(K key, long start, long end);
 移除指定索引位置的成员,其中有序集成员按分数值递增(从小到大)顺序排列*/
System.out.println(zSetoperations.range("zset2",0,-1));
System.out.println(zSetoperations.removeRange("zset2",1,2));
System.out.println(zSetoperations.range("zset2",0,-1));
//结果:[zset-1, zset-2, zset-3, zset-4]
//结果:2
//结果:[zset-1, zset-4]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐