Redis notes -------- use of Jedis, redis -------- jedis
During the Spring Festival, redo the system and try to use redis. Md. You can't bear it. Learn it again.
Redis installation and startup will not be mentioned
I. Preparations
1. redis-cli-p 6379
2. Create a project in eclipse and import the jedis package
3. Test jedis connectivity
Ii. Common Jedis APIs (hash)
1. Basic key value
Saves Chinese characters. Chinese characters are displayed on the eclipse console when data is retrieved, but Chinese characters are garbled back on linux (not garbled characters, but not Chinese characters we saved ). If you use redis-cli-raw, the above problems will not occur.
2. Hash: In general, the KV mode remains unchanged, but V is a key-value pair.
(1) hset
Description: sets the field value in the key field of the hash table to value. If the key does not exist, a new hash table is created and HSET is performed. If the field already exists in the hash table, the old value will be overwritten.
Parameter: key field value
Returned value: 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 is overwritten by the new value, 0 is returned.
Jedis. hset ("key1", "field1", "field1-value"); jedis. hset ("key1", "field2", "field2-value"); String content = jedis. hget ("key1", "field1"); // Use System for hget. out. println (content );
(2) hmset
Description: sets multiple field-value pairs to the hash table key at the same time. This command overwrites the existing fields in the hash table. If the key does not exist, an empty hash table is created and HMSET is executed.
Parameter: key field value [field value...]
Return Value: If the command is successfully executed, OK is returned. If the key type is not hash, an error is returned.
Map map = new HashMap(); map.put("field1", "field1-value"); map.put("field2", "field2-value"); jedis.hmset("key1", map); List<String> list = jedis.hmget("key1","field1");//hmget System.out.println(list);
(3) return all fields and values in the hash set specified by the key. In the return value, the next of each field name is its value, so the return value is twice the length of the hash set.
(4) hdel
(5) hlen
(6) hexists
(7) hkeys/hvals
Hgetall both returns the key and value in the hash table. hkeys returns the key in the hash table. hvals returns the value in the hash table.
(8) hincrby | hincrbyfloat
(9) hsetnx
Convert the hash tablekeyDomain infieldTovalue, When and only when the domainfieldDoes not exist.
If the domainfieldThe specified operation already exists.
IfkeyNo. A new hash table is created and executed with the hsetnx command.
-
Return Value:
-
Set successfully, return
1. If the specified domain already exists and no operation is executed 0.
Iii. Redis transactions
Discard cancel transaction
Exec execution
Multi start transaction
Unwatch: Disable the watch command to monitor all keys.
Watch monitors one or more keys. If the key is changed by other commands before the transaction is executed, the transaction will be interrupted.
Redis transaction phase:
(1) Enable: Start a transaction with multi
(2) join
(3) Execution: the transaction is triggered by the exec command.
Redis transactions do not guarantee atomicity: If a command fails to be executed in the same redis transaction, the subsequent commands will still be executed without rollback.
4. Jedis master-slave Replication
public class TestMS {public static void main(String[] args) {Jedis jedis_M = new Jedis("192.168.172.128",6379);Jedis jedis_S = new Jedis("192.168.172.128",6380);System.out.println(jedis_M.ping());System.out.println(jedis_S.ping());jedis_S.slaveof("192.168.172.128",6379);jedis_M.set("class","1122V2");String result = jedis_S.get("class");System.out.println(result);}}
If NULL exists, do not panic. Because the memory speed is too fast, just wait for a while to execute it.
5. JedisPool
package com.test;import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPool;import redis.clients.jedis.JedisPoolConfig;public class JedisPoolUtil {private static volatile JedisPool jedisPool = null;private JedisPoolUtil(){}public static JedisPool getJedisPoolInstance(){if(null == jedisPool){synchronized (JedisPoolUtil.class){if(null == jedisPool){JedisPoolConfig poolConfig = new JedisPoolConfig();poolConfig.setMaxActive(1000);poolConfig.setMaxIdle(32);poolConfig.setMaxWait(100*1000);poolConfig.setTestOnBorrow(true);jedisPool = new JedisPool(poolConfig,"127.0.0.1",6379);}}}return jedisPool;}public static void release(JedisPool jedisPool,Jedis jedis){if(null != jedis){jedisPool.returnResourceObject(jedis);}}}
package com.test;import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPool;public class TestPool {public static void main(String[] args) {JedisPool jedisPool = JedisPoolUtil.getJedisPoolInstance();JedisPool jedisPool2 = JedisPoolUtil.getJedisPoolInstance();System.out.println(jedisPool == jedisPool2);Jedis jedis = null;try {jedis = jedisPool.getResource();jedis.set("aa", "bb");} catch (Exception e) {e.printStackTrace();} finally {JedisPoolUtil.release(jedisPool, jedis);}}}