Cross reference table of redis client commands and redis template Java commands

- ich lebe . 2022-06-24 07:43:58 阅读数:1,002

crossreferencetableredisclient

Redis data type

Redis Five data types are supported :string( character string ),hash( Hash ),list( list ),set( aggregate ) And zset(sorted set: Ordered set ).

One 、Redis character string (String)

ValueOperations valueOperations = redisTemplate.opsForValue();
/**Set void*/
valueOperations.set("keyDemo","hello");
/**Set void The same key, Will be on the original value Make a copy , The set You can set the expiration time */
valueOperations.set("keyDemo","hello word",30, TimeUnit.SECONDS);
/**Set void( make carbon copies ) From previous value 6 Subscript begins to replicate */
valueOperations.set("keyDemo","redis",6);
// result : hello redis
/** If key Already exist , The value is appended to the end of the string . * If the key doesn't exist , Then it is created and set to an empty string */
valueOperations.append("keyDemo"," By");
valueOperations.append("keyDemo1","Hello");
// keyDemo result : hello redis By
// keyDemo1 result : Hello
/**size Long return key The corresponding value The length of */
System.out.println("****5key****:"+valueOperations.size("keyDemo"));
// result :14
/**getAndSet String First Key Corresponding value , And then you do the assignment */
System.out.println("****6key****:"+valueOperations.getAndSet("keyDemo","String Type is End!"));
// result hello redis By
System.out.println("****7key****:"+valueOperations.get("keyDemo"));
// result String Type is End!

Two 、Redis Hash (Hash)

/** Set expiration time */
redisTemplate.expire("redisHash",10, TimeUnit.SECONDS);
HashOperations hashOperations = redisTemplate.opsForHash();
/**void put(H key, HK hashKey, HV value); Set hash hashKey Value */
hashOperations.put("redisHash","name","666");
hashOperations.put("redisHash","age",26);
hashOperations.put("redisHash","class","6");
// result : result :{age=26, class=6, name=666}
/**void putAll(H key, Map<? extends HK, ? extends HV> m); Use m Multiple hash fields provided in are set to key In the corresponding hash table */
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"));
// result : result :{class=1, name=jack, age=27}
/**Set<HK> keys(H key); obtain key Of the corresponding hash table key*/
System.out.println(hashOperations.keys("redisHash"));
// result :redisHash The corresponding hash table is {class=1, name=666, age=27}
// result : result :[name, class, age]
/** * HV get(H key, Object hashKey); */
System.out.println(hashOperations.get("redisHash","age"));
// result : result :26
/** * Boolean hasKey(H key, Object hashKey); * Determine hash hashKey Whether there is */
System.out.println(hashOperations.hasKey("redisHash","666")); // result :true
System.out.println(hashOperations.hasKey("redisHash","777"));// result :false
/**List<HV> values(H key); Get the value of the entire hash store according to the key */
System.out.println(hashOperations.values("redisHash"));
// result :[666, 26, 6]
/**Map<HK, HV> entries(H key); Get the entire hash store according to the key */
System.out.println(hashOperations.entries("redisHash"));
// result :{age=26, class=6, name=666}
/** Cursor<Map.Entry<HK, HV>> scan(H key, ScanOptions options); Use Cursor stay key Of hash Middle iteration , Equivalent to iterator .*/
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());
}
// result :age:27 class:6 name:666
/** * Long delete(H key, Object... hashKeys); * Delete the given hash hashKeys */
System.out.println(hashOperations.delete("redisHash","name"));
System.out.println(hashOperations.entries("redisHash"));
// result :1
// result :{class=6, age=26}

3、 ... and 、Redis list (List)

/** Set expiration time */
redisTemplate.expire("redisHash",10, TimeUnit.SECONDS);
ListOperations listOperations = redisTemplate.opsForList();
/**leftPush Long * Inserts the specified value into the header of the list stored in the key .( Insert... From the left )*/
Long aLong = listOperations.leftPush("listKey","kyee"); // return 1
Long aLong1 = listOperations.leftPush("listKey","shinow");// return 2
Long aLong2 = listOperations.leftPushAll("listKey", "aa", "bb", "cc");
/** leftPushAll(K key, V... values) Long; Batch insert a batch of parameters into the list leftPushAll(Collection collection) Insert a set into List surface */
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 The two commands are similar to the above , No introduction */
/** set(K key, long index, V value) In the list index Set and overwrite the position of value value */
listOperations.set("listKey",2,"test");
// result :kyee,shinow,test,bb,cc, .....
/**Long remove(K key, long count, Object value); * count<0 Deleting the first one from the end to the end equals value The elements of * count=0 Delete all equals value The elements of * count>0 Delete the first from beginning to end equal to value The elements of * */
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); * Get the values in the list according to the following table */
listOperations.index("listKey",2);
/**Object leftPop(K key); Pop up the leftmost element , After pop-up, the value will no longer exist in the list */
listOperations.leftPop("listKey");
/** * leftPop(K key, long timeout, TimeUnit unit) * Remove the left element of the collection in the waiting time , If there is no element after waiting time, exit .*/
listOperations.leftPop("listKey",10,TimeUnit.SECONDS);
/**rightPop The two commands are similar to the above , No introduction */

Four 、Redis aggregate (Set)

SetOperations setOperations = redisTemplate.opsForSet();
/**Long add(K key, V... values); Add elements to the unordered collection , Return the number of additions You can also directly add Add multiple values Such as :template.opsForSet().add("setTest","aaa","bbb")*/
String[] strs= new String[]{
"str1","str2"};
System.out.println(setOperations.add("setTest", strs));
// result :2
/**Long remove(K key, Object... values); Remove one or more members of the collection */
System.out.println(setOperations.remove("setTest",strs));
// result :2
/**V pop(K key); Remove and return a random element from the collection */
System.out.println(setOperations.pop("setTest"));
System.out.println(setOperations.members("setTest"));
// result :bbb
// result : [aaa, ccc]
/**Boolean move(K key, V value, K destKey); take member Elements from source The assembly moves to destination aggregate */
setOperations.move("setTest","aaa","setTest2");
System.out.println(setOperations.members("setTest"));
System.out.println(setOperations.members("setTest2"));
// result :[ccc]
// result :[aaa]
/**Cursor<V> scan(K key, ScanOptions options); Traverse set*/
Cursor<Object> curosr = setOperations.scan("setTest", ScanOptions.NONE);
while(curosr.hasNext()){

System.out.println(curosr.next());
}

5、 ... and 、Redis Ordered set (sorted set)

ZSetOperations zSetOperations = redisTemplate.opsForZSet();
/** * Boolean add(K key, V value, double score); * Add an ordered set , The word of existence is false, If there is no such thing as true * ZSet The inside is according to score Sort * */
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));
// result :[zset-1, zset-2, zset-3, zset-4, zset-5, zset-6]
/**Long size(K key); Get the number of members of the ordered set , What's called internally is zCard Method */
System.out.println(zSetOperations.size("zset1"));
// result :6
/**Long remove(K key, Object... values); Remove one or more elements from an ordered collection */
System.out.println(zSetOperations.range("zset1",0,-1));
System.out.println(zSetOperations.remove("zset1","zset-6"));
System.out.println(zSetOperations.range("zset1",0,-1));
// result :[zset-1, zset-2, zset-3, zset-4, zset-5, zset-6]
// result : 1
// result :[zset-1, zset-2, zset-3, zset-4, zset-5]
/**Long rank(K key, Object o); Returns the ranking of specified members in an ordered set , The members of the ordered set are incremented by the fractional value ( From small to large ) Sequential arrangement */
System.out.println(zSetOperations.range("zset1",0,-1));
System.out.println(zSetOperations.rank("zset1","zset-2"));
// result :[zset-2, zset-1, zset-3, zset-4, zset-5]
// result :0 Show No.1 
/**Cursor<TypedTuple<V>> scan(K key, ScanOptions options); Traverse 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());
}
// result :zset-1:1.0
// result :zset-2:2.0
// result :zset-3:3.0
// result :zset-4:6.0
/**Long count(K key, double min, double max); Returns the number of members in the specified interval of an ordered set through scores */
System.out.println(zSetOperations.rangeByScore("zset1",0,5));
System.out.println(zSetOperations.count("zset1",0,5));
// result :[zset-2, zset-1, zset-3]
// result : 3
/**Double score(K key, Object o); Gets the... Of the specified member score value */
System.out.println(zSetOperations.score("zset1","zset-1"));
// result :2.2
/**Long removeRange(K key, long start, long end); Remove the members of the specified index location , The members of the ordered set are incremented by the fractional value ( From small to large ) Sequential arrangement */
System.out.println(zSetOperations.range("zset2",0,-1));
System.out.println(zSetOperations.removeRange("zset2",1,2));
System.out.println(zSetOperations.range("zset2",0,-1));
// result :[zset-1, zset-2, zset-3, zset-4]
// result :2
// result :[zset-1, zset-4]
copyright:author[- ich lebe .],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/175/202202211618494404.html