Writing in the front

  • See JAVA- Part 11 -Redis for other content

Jedis

  • Java operating redis
  • Simple operation
// connect to redis Jedis Jedis = new Jedis("127.0.0.1",6379); Set jedis.set("name","wangwu"); String name = jedis.get("name"); System.out.println(name); Jedis.close ();Copy the code
  • The method is similar to that on the console

hash

HashMap<String, String> map = new HashMap<>(); map.put("name","zhangsan"); map.put("age","18"); The map. The put (" weight ", "68.7"); jedis.hmset("hash",map); Map<String, String> hash = jedis.hgetAll("hash"); hash.forEach(new BiConsumer<String, String>() { @Override public void accept(String s, String s2) { System.out.println(s + "---" + s2); }});Copy the code

list

jedis.lpush("list", "a","b","c");
jedis.rpush("list", "a","b","c");
List<String> list = jedis.lrange("list", 0, -1);
for (String s : list) {
    System.out.println(s);
}
Copy the code

Encapsulated utility class

public class JedisUtils { private static String host; private static int port; private static int maxTotal; private static int maxIdle; private static JedisPool jp = null; static { ResourceBundle redis = ResourceBundle.getBundle("redis"); host = redis.getString("redis.host"); port = Integer.parseInt(redis.getString("redis.port")); maxTotal = Integer.parseInt(redis.getString("redis.maxTotal")); maxIdle = Integer.parseInt(redis.getString("redis.maxIdle")); JedisPoolConfig jpc = new JedisPoolConfig(); jpc.setMaxTotal(maxTotal); Jpc. setMaxIdle(maxIdle); jp = new JedisPool(jpc, host, port); } public static Jedis getJedis() { return jp.getResource(); }}Copy the code

RedisTemplate

  • Based on spring-boot, package guide and configuration are required
  • The command is similar to the console, which is only partially demonstrated below
  • The serialization format needs to be configured, otherwise there will be a prefix
@Bean public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate redisTemplate = new RedisTemplate(); / / to redis template to set up the connection factory, in setting up the serialization rules redisTemplate. SetConnectionFactory (redisConnectionFactory); / / set the serialization rules redisTemplate. SetKeySerializer (new StringRedisSerializer ()); redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); return redisTemplate; }Copy the code

string

  • Operations on String are used directlyStringRedisTemplate
/ / delete stringRedisTemplate. Delete (" name "); //set stringRedisTemplate.boundValueOps("name").set("zhangsan"); stringRedisTemplate.opsForValue().set("age","18"); //get String name = stringRedisTemplate.boundValueOps("name").get(); . / / the numerical change stringRedisTemplate boundValueOps (" age "). The increment (2 l); / / set the expiration time, unit stringRedisTemplate. BoundValueOps (" weight "). The set (" 80 ", 10 l, TimeUnit. SECONDS); . / / additional stringRedisTemplate boundValueOps (" name "), append (" dwadaw ");Copy the code

hash

HashMap<String, Object> user = new HashMap<>(); user.put("name","zhangsan"); user.put("age",20); user.put("weight",63); //put redisTemplate.boundHashOps("user").putAll(user); / / get all of the fields Set keys = redisTemplate. BoundHashOps (" user "). The keys (); System.out.println(keys); / / get all the List values. = redisTemplate boundHashOps (" user ") values (); System.out.println(values); / / the key value of the Map entries = redisTemplate. BoundHashOps (" user "). Entries (); System.out.println(entries); / / remove redisTemplate boundHashOps (" user "). The delete (" weight "); . / / whether there is a Boolean res = redisTemplate boundHashOps (" user "). HasKey (" weight "); System.out.println(res); / / put single redisTemplate boundHashOps (" user "). The put (" weight ", 300);Copy the code

list

BoundListOperations listKey = redisTemplate.boundListOps("listKey"); //push listKey.leftPushAll("zhangsan","lisi", "wangwu"); listKey.rightPushAll("zhangsan","lisi","wangwu"); List = listkey. range(0, -1); System.out.println(list); List list1 = redisTemplate.opsForList().range("listKey", 0,-1); System.out.println(list1); //pop Object o = listKey.leftPop(); System.out.println(o); Object o1 = listKey.rightPop(); System.out.println(o1); // Store data within 10 SECONDS Object O2 = listKey.leftPop(10L, timeunit.seconds); System.out.println(o2); Listkey. remove(1,"zhangsan"); listkey. remove(1,"zhangsan"); // Set listkey. set(3,"xiaoming"); Object index = listkey.index (0); System.out.println(index);Copy the code

set

  • The basic use
BoundSetOperations setKey = redisTemplate.boundSetOps("setKey"); //add setKey.add("name","age","weight"); // Get Set members = setkey.members (); System.out.println(members); //size Long size = setKey.size(); System.out.println(size); Boolean res = setkey. isMember("addr"); System.out.println(res); / / remove the setKey. Remove (" name ", "age");Copy the code
  • operation
BoundSetOperations setKey = redisTemplate.boundSetOps("setKey"); // Set res = setkey. union(" setkey2 "); // Save to setKey3 setkey. unionAndStore("setKey2","setKey3"); redisTemplate.opsForSet().unionAndStore("setKey","setKey2","setKey4"); SetKey5 HashSet<String> HashSet = new HashSet<>(); hashSet.add("setKey4"); hashSet.add("setKey3"); setKey.unionAndStore(hashSet,"setKey5");Copy the code

sorted_set

/ / add BoundZSetOperations zsetKey = redisTemplate. BoundZSetOps (" zsetKey "); zsetKey.add("zhangsan",80); zsetKey.add("lisi",100); zsetKey.add("wangwu",90); DefaultTypedTuple<String> p1 = new DefaultTypedTuple("wangwu1", 65.0); DefaultTypedTuple<String> p2 = new DefaultTypedTuple("wangwu2", 50.0); DefaultTypedTuple<String> p3 = new DefaultTypedTuple("wangwu3", 40.0); zsetKey.add(new HashSet<>(Arrays.asList(p1,p2,p3))); // Set range = zsetkey. range(0, -1); System.out.println(range); // Set set1 = zsetKey.reverseRange(0, -1); System.out.println(set1); //min Max Set Set = zsetKey.rangeByScore(75, 95); System.out.println(set); / / min Max descending Set set2 = zsetKey reverseRangeByScore (75, 95); System.out.println(set2); Set<DefaultTypedTuple<String>> tuples = zsetKey.rangeWithScores(0, -1); for (DefaultTypedTuple<String> tuple : tuples) { System.out.println(tuple.getValue() + " - " + tuple.getScore()); } // Get the specified Double resLisi = zsetkey.score ("lisi"); System.out.println(resLisi); Long count = zsetkey. count(75, 95); System.out.println(count); Remove ("wangwu3"); // remove zsetkey. remove("wangwu3"); Zsetkey.removerange (0,3); / / score range remove zsetKey removeRangeByScore (30, 60);Copy the code