For a long time, Redis, as a Nosql database, has been widely used in a variety of projects due to its function of frying chickens and cows. I didn't think much of Redis before, but now I like it more and more. It is often used to store some common data, such as user id, session, user menu permission, message queue, etc. Let's just summarize.
@Test public void pool() { JedisPoolConfig config = new JedisPoolConfig(); //maximum connection config.setMaxTotal(30); //Maximum number of idle connections config.setMaxIdle(2); JedisPool pool = new JedisPool(config, "192.168.101.3", 6379); Jedis jedis = null; try { jedis = pool.getResource(); jedis.set("name", "lisi"); String name = jedis.get("name"); System.out.println(name); }catch(Exception ex){ ex.printStackTrace(); }finally{ if(jedis != null){ //Close connection jedis.close(); } } }
1.Redis key operation
==It is often used to store fixed values or session s with little change==
/** * @return Number of deleted key s */ public Long delete(String... keys){ return jedis.del(keys); }
/** * Returns the type of value the key stores. */ public String type(String key){ return jedis.type(key); }
/** * Get key list */ public Set<String> keys(String pattern){ return jedis.keys(pattern); }
/** * View the remaining lifetime of a key: time to live (in seconds) */ public Long ttl(String key){ return jedis.ttl(key); }
/** * Check whether a key exists */ public Boolean exists(String key){ return jedis.exists(key); }
/** * Set the life time of a record, delete after expiration */ public Long expire(String key, int seconds){ return jedis.expire(key, seconds); }
2. String operation
==Commonly used to store some fixed key values (username: AAA, password: 111)==
/** * The value of a key will be set. If the key already holds other values, the old value will be overwritten and the type will be ignored. */ public String set(String key, String value){ return jedis.set(key, value); }
/** * Set the value of key to value. If and only if the key does not exist, SETNX will not take any action if the given key already exists. */ public Long setnx(String key, String value){ return jedis.setnx(key, value); }
/** * Associate the value value to the key and set the key's lifetime to seconds. */ public String setex(String key, int seconds, String value){ return jedis.setex(key, seconds, value); }
/** * Returns the string value associated with the key. */ public String get(String key){ return jedis.get(key); }
==Commonly used to count some total quantities==
/** * Increase the number value stored in the key by one. * If the key does not exist, the value of the key is initialized to 0 before the INCR operation. * If the value contains the wrong type, or the value of string type cannot be represented as a number, an error is returned. * @param key * @return */ public Long incr(String key){ return jedis.incr(key); }
==It is often used to increase the number of specific objects==
/** * Add the number value stored in the key to the fixed system. * If the key does not exist, the value of the key is initialized to 0 before the INCR operation. * If the value contains the wrong type, or the value of string type cannot be represented as a number, an error is returned. */ public Long incrBy(String key, long increase){ return jedis.incrBy(key, increase); }
/** * Reduce the number value stored in the key by one. * If the key does not exist, the value of the key is initialized to 0 before DECR. * Returns an error if the value contains the wrong type, or if the value of string type cannot be represented as a number */ public Long decr(String key){ return jedis.decr(key); }
/** * Reduce the number value stored in the key to a fixed value. * If the key does not exist, the value of the key is initialized to 0 before DECR. * If the value contains the wrong type, or the value of string type cannot be represented as a number, an error is returned. */ public Long decrBy(String key, long decrease){ return jedis.decrBy(key, decrease); }
3.Hash operation
==Not much==
/** * Set field value * @return If the field is a new field in the hash table and the value is set successfully, 1 is returned; if the field in the hash table already exists and the old value has been overwritten by the new value, 0 is returned. */ public Long hset(String hkey, String field, String value){ return jedis.hset(hkey, field, value); }
/** * Set the value of the domain field in the hash table key to value, if and only if the domain field does not exist * The operation is not valid if the domain field already exists. * If the key does not exist, a new hash table is created and HSETNX command is executed * @return Set successfully, return 1. Returns 0 if the given domain already exists and no operation is performed. */ public Long hsetnx(String hkey, String field, String value){ return jedis.hsetnx(hkey, field, value); }
/** * Set multiple field value pairs to the hash table key at the same time * This command overwrites fields that already exist in the hash table * If the key does not exist, an empty hash table is created and the HMSET operation is performed. */ public String hmset(String hkey, Map<String, String> hash){ return jedis.hmset(hkey, hash); }
/** * Returns the value of the given field in the hash table key. */ public String hget(String hkey, String field){ return jedis.hget(hkey, field); }
/** * Returns all fields and values in the hash table * @return Returns the field and the value of the field of the hash table in the form of a list */ public Map<String, String> hgetAll(String hkey){ return jedis.hgetAll(hkey); }
/** * Delete a field in the hash table. If the field does not exist, it will be ignored * @return Number of domains successfully removed, excluding ignored domains */ public Long hdel(String hkey, String field){ return jedis.hdel(hkey, field); }
/** * Returns the number of fields in the hash table * @return The number of domains in the hash table. When the key does not exist, 0 is returned. */ public Long hlen(String hkey){ return jedis.hlen(hkey); }
/** * Check whether the given field exists in the hash table key */ public Boolean hexists(String hkey, String field){ return jedis.hexists(hkey, field); }
/** * Add the increment to the value of the field in the hash table key */ public Long hincrBy(String hkey, String field, long increment){ return jedis.hincrBy(hkey, field, increment); }
/** * Return all fields in the hash table key */ public Set<String> hkeys(String hkey){ return jedis.hkeys(hkey); }
/** * Returns the values of all fields in the hash table key */ public List<String> hvals(String hkey){ return jedis.hvals(hkey); }
4.List operation
==Commonly used for message queue push message==
/** * * Insert a value value into the header of the list key * An error is returned when the key exists but is not a list type * @return The length of the list after the LPUSH command is executed */ public Long lpush(String lkey, String value){ return jedis.lpush(lkey, value); }
/** * Insert a value value into the end of the list key (far right) * An error is returned when the key exists but is not a list type * @return Length of list after RPUSH operation */ public Long rpush(String lkey, String value){ return jedis.rpush(lkey, value); }
==Commonly used for message queue to get messages==
/** * Remove and return the header element of the list key * @return The header element of the list */ public String lpop(String lkey){ return jedis.lpop(lkey); }
/** * Remove and return the tail element of the list key * @return End element of list */ public String rpop(String lkey){ return jedis.rpop(lkey); }
==Commonly used to count the number of messages==
/** * @return Returns the length of the list */ public Long llen(String lkey){ return jedis.llen(lkey); }
/** * Returns the elements in the specified interval in the list, and the interval is specified by the offsets start and end * You can also use a negative subscript, where - 1 represents the last element of the list, - 2 represents the penultimate element of the list, and so on * @return */ public List<String> lrange(String lkey, int start, int end){ return jedis.lrange(lkey, start, end); }
/** * Based on the value of the parameter count, remove the elements in the list that are equal to the parameter value * count = 0 : Remove all values in the table equal to value * count > 0 : Search from the header to the footer, and remove the elements equal to value. The number is count * count < 0 : Search from the end of the table to the header, remove the elements equal to value, and the quantity is the absolute value of count * @return Number of elements removed */ public Long lrem(String lkey, int count, String value){ return jedis.lrem(lkey, count, value); }
/** * Set the value of the element whose list key subscript is index to value */ public String lset(String lkey, long index, String value){ return jedis.lset(lkey, index, value); }
5.Set operation
/** * Add one or more member elements to the set key, and the member elements that already exist in the set will be ignored * If the key does not exist, create a collection containing only the member element as a member * An error is returned when the key is not a set type * @return The number of new elements added to the collection, excluding ignored elements */ public Long sadd(String skey, String member){ return jedis.sadd(skey, member); }
/** * Remove one or more member elements from the set key. The non-existent member elements will be ignored * @return The number of elements that were successfully removed, excluding elements that were ignored. */ public Long srem(String skey, String member){ return jedis.srem(skey, member); }
/** * Return all members in the set key * Nonexistent key s are treated as empty sets * @return All members in the collection */ public Set<String> smembers(String skey){ return jedis.smembers(skey); }
/** * Judge whether the member element sets the members of key * @return Returns true if the member element is a member of the collection */ public Boolean sismember(String skey, String member){ return jedis.sismember(skey, member); }
/** * Returns the cardinality of the set key (the number of elements in the set) * @return Cardinality of set */ public Long scard(String skey){ return jedis.scard(skey); }
/** * Remove and return an element in the collection * @return Removed elements */ public String spop(String skey){ return jedis.spop(skey); }
==It is often used to do the analysis and comparison of two kinds of information, the intersection of Union and other mathematical things==
/** * Returns all members of a collection that is the intersection of all given collections * When there is an empty set in a given set, the result is also an empty set (according to the set operation law) * @return List of intersection members */ public Set<String> sinter(String... skeys){ return jedis.sinter(skeys); }
/** * Returns all the members of a collection, which is the union of all the given collections * @return List of union members */ public Set<String> sunion(String... skeys){ return jedis.sunion(skeys); }
/** * Returns all members of a set that is the difference between all given sets * @return List of intersection members */ public Set<String> sdiff(String... skeys){ return jedis.sdiff(skeys); }
6. Orderly collection operation
==Often used to add ranking members==
/** * Add a member element and its score value to the ordered set key * @return The number of new members added successfully, excluding those updated and existing members */ public Long zadd(String zkey, double score, String member){ return jedis.zadd(zkey, score, member); }
==Often used to delete ranking members==
/** * Remove one or more member elements from the set key. The non-existent member elements will be ignored * When the key is not a set type, an error is returned * @return Number of elements removed successfully, excluding ignored elements */ public Long zrem(String zkey, String member){ return jedis.zrem(zkey, member); }
/** * Returns the cardinality of an ordered set key * @return Returns the cardinality of the ordered set when the key exists and is of the ordered set type; returns 0 when the key does not exist */ public Long zcard(String zkey){ return jedis.zcard(zkey); }
==It is often used to count the number of members in a certain range==
/** * Returns the members of the ordered set key whose score value is between min and max (including the members whose score value is equal to min or max by default) * @return score Number of members with values between min and max */ public Long zcount(String zkey, double min, double max){ return jedis.zcount(zkey, min, max); }
==Used to count the scores of a member in a leaderboard==
/** * Returns the score value of the member in the ordered set key * @return member Member's score value */ public Double zscore(String zkey, String member){ return jedis.zscore(zkey, member); }
==Often used to add value to a member in a leaderboard==
/** * Add the increment to the score value of the member of the ordered set key * @return member New score value for member */ public Double zincrby(String zkey, double increment, String member){ return jedis.zincrby(zkey, increment, member); }
==Used in ranking statistics from small to top==
/** * Returns the members in the specified interval in the ordered set key * The positions of members are sorted by increasing the score value (from small to large) * You can also use negative subscripts, with - 1 for the last member, - 2 for the last member, and so on * @return List of members of an ordered set in a specified interval */ public Set<String> zrange(String zkey, int start, int end){ return jedis.zrange(zkey, start, end); }
==Commonly used in ranking statistics from top to bottom==
/** * Returns the members in the specified interval in the ordered set key * Where the positions of members are sorted by decreasing the score value (from large to small) * You can also use negative subscripts, with - 1 for the last member, - 2 for the last member, and so on * @return List of members of an ordered set in a specified interval */ public Set<String> zrevrange(String zkey, int start, int end){ return jedis.zrevrange(zkey, start, end); }
/** * Returns all members of the ordered set key whose score value is between min and max (including equal to min or max). Members of an ordered set are arranged in ascending (small to large) order of the score value. * @param zkey * @param min * @param max * @return */ public Set<String> zrangeByScore(String zkey, double min, double max){ return jedis.zrangeByScore(zkey, min, max); }
/** * Returns the ranking of members in the ordered set key. The members of the ordered set are arranged in the order of increasing score value (from small to large). * @return If member is a member of the ordered set key, return the member ranking */ public Long zrank(String zkey, String member){ return jedis.zrank(zkey, member); }
/** * Returns the ranking of members in the ordered set key. The ordered set members are sorted in descending order (from large to small) according to the score value * @return If member is a member of the ordered set key, return the member ranking */ public Long zrevrank(String zkey, String member){ return jedis.zrevrank(zkey, member); }
/** * Remove all members of the ordered set key whose score value is between min and max (including equal to min or max). * @return Number of members removed */ public Long zremrangeByScore(String zkey, double start, double end){ return jedis.zremrangeByScore(zkey, start, end); }
/** * Remove all members of the ordered set key whose score value is between min and max (including equal to min or max). * @param zkey * @param start When the minimum value is not known, - inf can be used instead of the minimum value * @param end When the maximum value is not known, + inf can be used instead of the maximum value */ public Long zremrangeByScore(String zkey, String start, String end){ return jedis.zremrangeByScore(zkey, start, end); }
7. Transaction operation
/** * Multiple commands within a transaction block are put into a queue in order, and are executed atomically by EXEC commands. * @param block */ public List<Object> multiResult(TransactionBlock block){ return jedis.multi(block); }
/* * Mark the beginning of a transaction block * @return Transaction Object, which can be based on Transaction operations */ public Transaction multi(){ return jedis.multi(); }
After reading these, can you say that you can fully use these API s??????
- [atomic]: meaning of atomicity (to test MarkDown's comment, please ignore)
- [multi]: meaning of majority (please ignore the mark down comment)
- [Transaction]: the meaning of the Transaction (for testing MarkDown comments, please ignore)