Home Database Redis Detailed explanation of Jedis' operation of redis

Detailed explanation of Jedis' operation of redis

Jan 20, 2021 pm 05:54 PM
jedis redis

redisThis column mainly explains the five major types of operations of Jedis on redis: strings, lists, hashes, sets, and ordered sets.

Detailed explanation of Jedis' operation of redis

Recommended: redis tutorial (free)

##JedisUtil
Test cases here Use junit4 to run, and prepare the code as follows:

    private static final String ipAddr = "10.10.195.112";
    private static final int port = 6379;
    private static Jedis jedis= null;

    @BeforeClass
    public static void init()
    {
        jedis = JedisUtil.getInstance().getJedis(ipAddr, port);
    }

    @AfterClass
    public static void close()
    {
        JedisUtil.getInstance().closeJedis(jedis,ipAddr, port);
    }
Copy after login

Among them, JedisUtil is a simple encapsulation of jedis, the code is as follows:

import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Map;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class JedisUtil
{
    private Logger logger = Logger.getLogger(this.getClass().getName());

    private JedisUtil(){}

    private static class RedisUtilHolder{
        private static final JedisUtil instance = new JedisUtil();
    }

    public static JedisUtil getInstance(){
        return RedisUtilHolder.instance;
    }

    private static Map<String,JedisPool> maps = new HashMap<String,JedisPool>();

    private static JedisPool getPool(String ip, int port){
        String key = ip+":"+port;
        JedisPool pool = null;
        if(!maps.containsKey(key))
        {
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxActive(RedisConfig.MAX_ACTIVE);
            config.setMaxIdle(RedisConfig.MAX_IDLE);
            config.setMaxWait(RedisConfig.MAX_WAIT);
            config.setTestOnBorrow(true);
            config.setTestOnReturn(true);

            pool = new JedisPool(config,ip,port,RedisConfig.TIMEOUT);
            maps.put(key, pool);
        }
        else
        {
            pool = maps.get(key);
        }
        return pool;        
    }

    public Jedis getJedis(String ip, int port)
    {
        Jedis jedis = null;
        int count = 0;
        do
        {
            try
            {
                jedis = getPool(ip,port).getResource();
            }
            catch (Exception e)
            {
                logger.error("get redis master1 failed!",e);
                getPool(ip,port).returnBrokenResource(jedis);
            }
        }
        while(jedis == null && count<RedisConfig.RETRY_NUM);
        return jedis;
    }

    public void closeJedis(Jedis jedis, String ip, int port){
        if(jedis != null)
        {
            getPool(ip,port).returnResource(jedis);
        }
    }
}

public class RedisConfig
{
    //可用连接实例的最大数目,默认值为8;
    //如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
    public static int MAX_ACTIVE = 1024;

    //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。
    public static int MAX_IDLE = 200;

    //等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException;
    public static int MAX_WAIT = 10000;

    public static int TIMEOUT = 10000;

    public static int RETRY_NUM = 5;
}
Copy after login

##Key operation

    @Test public void testKey() throws InterruptedException
    {
        System.out.println("清空数据:"+jedis.flushDB());
        System.out.println("判断某个键是否存在:"+jedis.exists("username"));
        System.out.println("新增<&#39;username&#39;,&#39;zzh&#39;>的键值对:"+jedis.set("username", "zzh"));
        System.out.println(jedis.exists("name"));
        System.out.println("新增<&#39;password&#39;,&#39;password&#39;>的键值对:"+jedis.set("password", "password"));
        System.out.print("系统中所有的键如下:");
        Set<String> keys = jedis.keys("*");
        System.out.println(keys);
        System.out.println("删除键password:"+jedis.del("password"));
        System.out.println("判断键password是否存在:"+jedis.exists("password"));
        System.out.println("设置键username的过期时间为5s:"+jedis.expire("username", 5));
        TimeUnit.SECONDS.sleep(2);
        System.out.println("查看键username的剩余生存时间:"+jedis.ttl("username"));
        System.out.println("移除键username的生存时间:"+jedis.persist("username"));
        System.out.println("查看键username的剩余生存时间:"+jedis.ttl("username"));
        System.out.println("查看键username所存储的值的类型:"+jedis.type("username"));
    }
Copy after login

Output result:

清空数据:OK
判断某个键是否存在:false
新增<&#39;username&#39;,&#39;zzh&#39;>的键值对:OK
false
新增<&#39;password&#39;,&#39;password&#39;>的键值对:OK
系统中所有的键如下:[username, password]
删除键password:1
判断键password是否存在:false
设置键username的过期时间为5s:1
查看键username的剩余生存时间:3
移除键username的生存时间:1
查看键username的剩余生存时间:-1
查看键username所存储的值的类型:string
Copy after login

##String operations
In Redis, strings can store three types of values:

  • byte string(byte string)
  • Integer
  • Floating point number

Byte string

    @Test public void testString() throws InterruptedException
    {
        jedis.flushDB();
        System.out.println("===========增加数据===========");
        System.out.println(jedis.set("key1","value1"));
        System.out.println(jedis.set("key2","value2"));
        System.out.println(jedis.set("key3", "value3"));
        System.out.println("删除键key2:"+jedis.del("key2"));
        System.out.println("获取键key2:"+jedis.get("key2"));
        System.out.println("修改key1:"+jedis.set("key1", "value1Changed"));
        System.out.println("获取key1的值:"+jedis.get("key1"));
        System.out.println("在key3后面加入值:"+jedis.append("key3", "End"));
        System.out.println("key3的值:"+jedis.get("key3"));
        System.out.println("增加多个键值对:"+jedis.mset("key01","value01","key02","value02","key03","value03"));
        System.out.println("获取多个键值对:"+jedis.mget("key01","key02","key03"));
        System.out.println("获取多个键值对:"+jedis.mget("key01","key02","key03","key04"));
        System.out.println("删除多个键值对:"+jedis.del(new String[]{"key01","key02"}));
        System.out.println("获取多个键值对:"+jedis.mget("key01","key02","key03"));

        jedis.flushDB();
        System.out.println("===========新增键值对防止覆盖原先值==============");
        System.out.println(jedis.setnx("key1", "value1"));
        System.out.println(jedis.setnx("key2", "value2"));
        System.out.println(jedis.setnx("key2", "value2-new"));
        System.out.println(jedis.get("key1"));
        System.out.println(jedis.get("key2"));

        System.out.println("===========新增键值对并设置有效时间=============");
        System.out.println(jedis.setex("key3", 2, "value3"));
        System.out.println(jedis.get("key3"));
        TimeUnit.SECONDS.sleep(3);
        System.out.println(jedis.get("key3"));

        System.out.println("===========获取原值,更新为新值==========");//GETSET is an atomic set this value and return the old value command.
        System.out.println(jedis.getSet("key2", "key2GetSet"));
        System.out.println(jedis.get("key2"));

        System.out.println("获得key2的值的字串:"+jedis.getrange("key2", 2, 4));
    }
Copy after login
Output result:
===========增加数据===========
OK
OK
OK
删除键key2:1
获取键key2:null
修改key1:OK
获取key1的值:value1Changed
在key3后面加入值:9
key3的值:value3End
增加多个键值对:OK
获取多个键值对:[value01, value02, value03]
获取多个键值对:[value01, value02, value03, null]
删除多个键值对:2
获取多个键值对:[null, null, value03]
===========新增键值对防止覆盖原先值==============
1
1
0
value1
value2
===========新增键值对并设置有效时间=============
OK
value3
null
===========获取原值,更新为新值==========
value2
key2GetSet
获得key2的值的字串:y2G
Copy after login

memcached and redis also have the append operation, but memcached has the prepend operation, which is not available in redis.

Integers and floating point numbers

    @Test public void testNumber()
    {
        jedis.flushDB();
        jedis.set("key1", "1");
        jedis.set("key2", "2");
        jedis.set("key3", "2.3");
        System.out.println("key1的值:"+jedis.get("key1"));
        System.out.println("key2的值:"+jedis.get("key2"));
        System.out.println("key1的值加1:"+jedis.incr("key1"));
        System.out.println("获取key1的值:"+jedis.get("key1"));
        System.out.println("key2的值减1:"+jedis.decr("key2"));
        System.out.println("获取key2的值:"+jedis.get("key2"));
        System.out.println("将key1的值加上整数5:"+jedis.incrBy("key1", 5));
        System.out.println("获取key1的值:"+jedis.get("key1"));
        System.out.println("将key2的值减去整数5:"+jedis.decrBy("key2", 5));
        System.out.println("获取key2的值:"+jedis.get("key2"));
    }
Copy after login
Output results:

key1的值:1
key2的值:2
key1的值加1:2
获取key1的值:2
key2的值减1:1
获取key2的值:1
将key1的值加上整数5:7
获取key1的值:7
将key2的值减去整数5:-4
获取key2的值:-4
Copy after login
In redis2.6 or above There is this command: incrbyfloat, which adds the floating point amount to the value stored in the key. This operation is not supported in jedis-2.1.0.



##List @Test public void testList()
{
jedis.flushDB(); System.out.println("=Add a list

=");
jedis.lpush("collections", "ArrayList" , "Vector", "Stack", "HashMap", "WeakHashMap", "LinkedHashMap");
jedis.lpush("collections", "HashSet");
jedis.lpush("collections", " TreeSet");
jedis.lpush("collections", "TreeMap");
System.out.println("Contents of collections:" jedis.lrange("collections", 0, -1)); //-1 represents the penultimate element, -2 represents the penultimate element System.out.println("Elements in collections range 0-3:" jedis.lrange("collections",0,3) ); System.out.println("=
##");
// Delete the value specified in the list. The second parameter is the number of deleted items (when there are duplicates). The value added later is deleted first, similar to popping from the stack.
System.out.println("Delete the specified number of elements:" jedis.lrem("collections", 2, "HashMap"));
System.out.println("Contents of collections:" jedis. lrange("collections", 0, -1));
System.out.println("Delete elements outside the range 0-3 in the following table:" jedis.ltrim("collections", 0, 3));
System.out.println("Contents of collections:" jedis.lrange("collections", 0, -1));
System.out.println("Collections list popping off the stack (left end):" jedis .lpop(“collections”));
System.out.println(“Contents of collections:” jedis.lrange(“collections”, 0, -1));
System.out.println(“collections Add elements, from the right end of the list, corresponding to lpush:" jedis.rpush("collections", "EnumMap"));
System.out.println("Contents of collections:" jedis.lrange("collections", 0, -1));
System.out.println("Collections list pop (right end):" jedis.rpop("collections"));
System.out.println("Contents of collections: ” jedis.lrange(“collections”, 0, -1));
System.out.println(“Modify the content of collections specified subscript 1:” jedis.lset(“collections”, 1, “LinkedArrayList”) );
System.out.println("Contents of collections:" jedis.lrange("collections", 0, -1));
System.out.println("
#=");
System.out.println( "The length of collections:" jedis.llen("collections"));
System.out.println("Get the element with collections index 2:" jedis.lindex("collections", 2));
System.out.println("===============================");
jedis.lpush(" sortedList", "3", "6", "2", "0", "7", "4");
System.out.println("sortedList before sorting:" jedis.lrange("sortedList" , 0, -1));
System.out.println(jedis.sort("sortedList"));
System.out.println("After sortedList is sorted:" jedis.lrange("sortedList", 0, -1));
}
Output result:

===========添加一个list===========
collections的内容:[TreeMap, TreeSet, HashSet, LinkedHashMap, WeakHashMap, HashMap, Stack, Vector, ArrayList]
collections区间0-3的元素:[TreeMap, TreeSet, HashSet, LinkedHashMap]
===============================
删除指定元素个数:1
collections的内容:[TreeMap, TreeSet, HashSet, LinkedHashMap, WeakHashMap, Stack, Vector, ArrayList]
删除下表0-3区间之外的元素:OK
collections的内容:[TreeMap, TreeSet, HashSet, LinkedHashMap]
collections列表出栈(左端):TreeMap
collections的内容:[TreeSet, HashSet, LinkedHashMap]
collections添加元素,从列表右端,与lpush相对应:4
collections的内容:[TreeSet, HashSet, LinkedHashMap, EnumMap]
collections列表出栈(右端):EnumMap
collections的内容:[TreeSet, HashSet, LinkedHashMap]
修改collections指定下标1的内容:OK
collections的内容:[TreeSet, LinkedArrayList, LinkedHashMap]
===============================
collections的长度:3
获取collections下标为2的元素:LinkedHashMap
===============================
sortedList排序前:[4, 7, 0, 2, 6, 3]
[0, 2, 3, 4, 6, 7]
sortedList排序后:[4, 7, 0, 2, 6, 3]
Copy after login

Redis also has blocking list popping commands and commands for moving elements between lists: blpop, brpop , rpoplpush, brpoplpush, etc.

##Set

    @Test public void testSet()
    {
        jedis.flushDB();
        System.out.println("============向集合中添加元素============");
        System.out.println(jedis.sadd("eleSet", "e1","e2","e4","e3","e0","e8","e7","e5"));
        System.out.println(jedis.sadd("eleSet", "e6"));
        System.out.println(jedis.sadd("eleSet", "e6"));
        System.out.println("eleSet的所有元素为:"+jedis.smembers("eleSet"));
        System.out.println("删除一个元素e0:"+jedis.srem("eleSet", "e0"));
        System.out.println("eleSet的所有元素为:"+jedis.smembers("eleSet"));
        System.out.println("删除两个元素e7和e6:"+jedis.srem("eleSet", "e7","e6"));
        System.out.println("eleSet的所有元素为:"+jedis.smembers("eleSet"));
        System.out.println("随机的移除集合中的一个元素:"+jedis.spop("eleSet"));
        System.out.println("随机的移除集合中的一个元素:"+jedis.spop("eleSet"));
        System.out.println("eleSet的所有元素为:"+jedis.smembers("eleSet"));
        System.out.println("eleSet中包含元素的个数:"+jedis.scard("eleSet"));
        System.out.println("e3是否在eleSet中:"+jedis.sismember("eleSet", "e3"));
        System.out.println("e1是否在eleSet中:"+jedis.sismember("eleSet", "e1"));
        System.out.println("e1是否在eleSet中:"+jedis.sismember("eleSet", "e5"));
        System.out.println("=================================");
        System.out.println(jedis.sadd("eleSet1", "e1","e2","e4","e3","e0","e8","e7","e5"));
        System.out.println(jedis.sadd("eleSet2", "e1","e2","e4","e3","e0","e8"));
        System.out.println("将eleSet1中删除e1并存入eleSet3中:"+jedis.smove("eleSet1", "eleSet3", "e1"));
        System.out.println("将eleSet1中删除e2并存入eleSet3中:"+jedis.smove("eleSet1", "eleSet3", "e2"));
        System.out.println("eleSet1中的元素:"+jedis.smembers("eleSet1"));
        System.out.println("eleSet3中的元素:"+jedis.smembers("eleSet3"));
        System.out.println("============集合运算=================");
        System.out.println("eleSet1中的元素:"+jedis.smembers("eleSet1"));
        System.out.println("eleSet2中的元素:"+jedis.smembers("eleSet2"));
        System.out.println("eleSet1和eleSet2的交集:"+jedis.sinter("eleSet1","eleSet2"));
        System.out.println("eleSet1和eleSet2的并集:"+jedis.sunion("eleSet1","eleSet2"));
        System.out.println("eleSet1和eleSet2的差集:"+jedis.sdiff("eleSet1","eleSet2"));//eleSet1中有,eleSet2中没有
    }
Copy after login

Output result:

============向集合中添加元素============
8
1
0
eleSet的所有元素为:[e3, e4, e1, e2, e0, e8, e7, e6, e5]
删除一个元素e0:1
eleSet的所有元素为:[e3, e4, e1, e2, e8, e7, e6, e5]
删除两个元素e7和e6:2
eleSet的所有元素为:[e3, e4, e1, e2, e8, e5]
随机的移除集合中的一个元素:e5
随机的移除集合中的一个元素:e2
eleSet的所有元素为:[e3, e4, e1, e8]
eleSet中包含元素的个数:4
e3是否在eleSet中:true
e1是否在eleSet中:true
e1是否在eleSet中:false
=================================
8
6
将eleSet1中删除e1并存入eleSet3中:1
将eleSet1中删除e2并存入eleSet3中:1
eleSet1中的元素:[e3, e4, e0, e8, e7, e5]
eleSet3中的元素:[e1, e2]
============集合运算=================
eleSet1中的元素:[e3, e4, e0, e8, e7, e5]
eleSet2中的元素:[e3, e4, e1, e2, e0, e8]
eleSet1和eleSet2的交集:[e3, e4, e0, e8]
eleSet1和eleSet2的并集:[e3, e4, e1, e2, e0, e8, e7, e5]
eleSet1和eleSet2的差集:[e7, e5]
Copy after login

There are some other commands about Set: srandmember, sdiffstore, sinterstore, sunionstore wait.

##Hash

    @Test public void testHash()
    {
        jedis.flushDB();
        Map<String,String> map = new HashMap<>();
        map.put("key1","value1");
        map.put("key2","value2");
        map.put("key3","value3");
        map.put("key4","value4");
        jedis.hmset("hash",map);
        jedis.hset("hash", "key5", "value5");
        System.out.println("散列hash的所有键值对为:"+jedis.hgetAll("hash"));//return Map<String,String>
        System.out.println("散列hash的所有键为:"+jedis.hkeys("hash"));//return Set<String>
        System.out.println("散列hash的所有值为:"+jedis.hvals("hash"));//return List<String>
        System.out.println("将key6保存的值加上一个整数,如果key6不存在则添加key6:"+jedis.hincrBy("hash", "key6", 6));
        System.out.println("散列hash的所有键值对为:"+jedis.hgetAll("hash"));
        System.out.println("将key6保存的值加上一个整数,如果key6不存在则添加key6:"+jedis.hincrBy("hash", "key6", 3));
        System.out.println("散列hash的所有键值对为:"+jedis.hgetAll("hash"));
        System.out.println("删除一个或者多个键值对:"+jedis.hdel("hash", "key2"));
        System.out.println("散列hash的所有键值对为:"+jedis.hgetAll("hash"));
        System.out.println("散列hash中键值对的个数:"+jedis.hlen("hash"));
        System.out.println("判断hash中是否存在key2:"+jedis.hexists("hash","key2"));
        System.out.println("判断hash中是否存在key3:"+jedis.hexists("hash","key3"));
        System.out.println("获取hash中的值:"+jedis.hmget("hash","key3"));
        System.out.println("获取hash中的值:"+jedis.hmget("hash","key3","key4"));
    }
Copy after login

Output result:

散列hash的所有键值对为:{key4=value4, key3=value3, key5=value5, key2=value2, key1=value1}
散列hash的所有键为:[key4, key3, key5, key2, key1]
散列hash的所有值为:[value4, value3, value1, value2, value5]
将key6保存的值加上一个整数,如果key6不存在则添加key6:6
散列hash的所有键值对为:{key4=value4, key3=value3, key6=6, key5=value5, key2=value2, key1=value1}
将key6保存的值加上一个整数,如果key6不存在则添加key6:9
散列hash的所有键值对为:{key4=value4, key3=value3, key6=9, key5=value5, key2=value2, key1=value1}
删除一个或者多个键值对:1
散列hash的所有键值对为:{key4=value4, key3=value3, key6=9, key5=value5, key1=value1}
散列hash中键值对的个数:5
判断hash中是否存在key2:false
判断hash中是否存在key3:true
获取hash中的值:[value3]
获取hash中的值:[value3, value4]
Copy after login

##Ordered set

    @Test public void testSortedSet()
    {
        jedis.flushDB();
        Map<Double,String> map = new HashMap<>();
        map.put(1.2,"key2");
        map.put(4.0, "key3");
        map.put(5.0,"key4");
        map.put(0.2,"key5");
        System.out.println(jedis.zadd("zset", 3,"key1"));
        System.out.println(jedis.zadd("zset",map));
        System.out.println("zset中的所有元素:"+jedis.zrange("zset", 0, -1));
        System.out.println("zset中的所有元素:"+jedis.zrangeWithScores("zset", 0, -1));
        System.out.println("zset中的所有元素:"+jedis.zrangeByScore("zset", 0,100));
        System.out.println("zset中的所有元素:"+jedis.zrangeByScoreWithScores("zset", 0,100));
        System.out.println("zset中key2的分值:"+jedis.zscore("zset", "key2"));
        System.out.println("zset中key2的排名:"+jedis.zrank("zset", "key2"));
        System.out.println("删除zset中的元素key3:"+jedis.zrem("zset", "key3"));
        System.out.println("zset中的所有元素:"+jedis.zrange("zset", 0, -1));
        System.out.println("zset中元素的个数:"+jedis.zcard("zset"));
        System.out.println("zset中分值在1-4之间的元素的个数:"+jedis.zcount("zset", 1, 4));
        System.out.println("key2的分值加上5:"+jedis.zincrby("zset", 5, "key2"));
        System.out.println("key3的分值加上4:"+jedis.zincrby("zset", 4, "key3"));
        System.out.println("zset中的所有元素:"+jedis.zrange("zset", 0, -1));
    }
Copy after login

Output result:

1
4
zset中的所有元素:[key5, key2, key1, key3, key4]
zset中的所有元素:[[[107, 101, 121, 53],0.2], [[107, 101, 121, 50],1.2], [[107, 101, 121, 49],3.0], [[107, 101, 121, 51],4.0], [[107, 101, 121, 52],5.0]]
zset中的所有元素:[key5, key2, key1, key3, key4]
zset中的所有元素:[[[107, 101, 121, 53],0.2], [[107, 101, 121, 50],1.2], [[107, 101, 121, 49],3.0], [[107, 101, 121, 51],4.0], [[107, 101, 121, 52],5.0]]
zset中key2的分值:1.2
zset中key2的排名:1
删除zset中的元素key3:1
zset中的所有元素:[key5, key2, key1, key4]
zset中元素的个数:4
zset中分值在1-4之间的元素的个数:2
key2的分值加上5:6.2
key3的分值加上4:4.0
zset中的所有元素:[key5, key1, key3, key4, key2]
Copy after login

Ordered sets also have commands such as zinterstore, zunionstore, zremrangebyscore, zremrangebyrank, zrevrank, zrevrange, zrangebyscore and other commands.

##Sort sort

    @Test public void testSort()
    {
        jedis.flushDB();
        jedis.lpush("collections", "ArrayList", "Vector", "Stack", "HashMap", "WeakHashMap", "LinkedHashMap");
        System.out.println("collections的内容:"+jedis.lrange("collections", 0, -1));
        SortingParams sortingParameters = new SortingParams();
        System.out.println(jedis.sort("collections",sortingParameters.alpha()));
        System.out.println("===============================");
        jedis.lpush("sortedList", "3","6","2","0","7","4");
        System.out.println("sortedList排序前:"+jedis.lrange("sortedList", 0, -1));
        System.out.println("升序:"+jedis.sort("sortedList", sortingParameters.asc()));
        System.out.println("升序:"+jedis.sort("sortedList", sortingParameters.desc()));
        System.out.println("===============================");
        jedis.lpush("userlist", "33");  
        jedis.lpush("userlist", "22");  
        jedis.lpush("userlist", "55");  
        jedis.lpush("userlist", "11");  
        jedis.hset("user:66", "name", "66");  
        jedis.hset("user:55", "name", "55");  
        jedis.hset("user:33", "name", "33");  
        jedis.hset("user:22", "name", "79");  
        jedis.hset("user:11", "name", "24");  
        jedis.hset("user:11", "add", "beijing");  
        jedis.hset("user:22", "add", "shanghai");  
        jedis.hset("user:33", "add", "guangzhou");  
        jedis.hset("user:55", "add", "chongqing");  
        jedis.hset("user:66", "add", "xi'an");  
        sortingParameters = new SortingParams();
        sortingParameters.get("user:*->name");  
        sortingParameters.get("user:*->add"); 
        System.out.println(jedis.sort("userlist",sortingParameters));
    }
Copy after login

Output result:

collections的内容:[LinkedHashMap, WeakHashMap, HashMap, Stack, Vector, ArrayList]
[ArrayList, HashMap, LinkedHashMap, Stack, Vector, WeakHashMap]
===============================
sortedList排序前:[4, 7, 0, 2, 6, 3]
升序:[0, 2, 3, 4, 6, 7]
升序:[7, 6, 4, 3, 2, 0]
===============================
[24, beijing, 79, shanghai, 33, guangzhou, 55, chongqing]
Copy after login

The above is the detailed content of Detailed explanation of Jedis' operation of redis. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to build the redis cluster mode How to build the redis cluster mode Apr 10, 2025 pm 10:15 PM

Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

How to clear redis data How to clear redis data Apr 10, 2025 pm 10:06 PM

How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

How to read redis queue How to read redis queue Apr 10, 2025 pm 10:12 PM

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

How to use the redis command How to use the redis command Apr 10, 2025 pm 08:45 PM

Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

How to use redis lock How to use redis lock Apr 10, 2025 pm 08:39 PM

Using Redis to lock operations requires obtaining the lock through the SETNX command, and then using the EXPIRE command to set the expiration time. The specific steps are: (1) Use the SETNX command to try to set a key-value pair; (2) Use the EXPIRE command to set the expiration time for the lock; (3) Use the DEL command to delete the lock when the lock is no longer needed.

How to read the source code of redis How to read the source code of redis Apr 10, 2025 pm 08:27 PM

The best way to understand Redis source code is to go step by step: get familiar with the basics of Redis. Select a specific module or function as the starting point. Start with the entry point of the module or function and view the code line by line. View the code through the function call chain. Be familiar with the underlying data structures used by Redis. Identify the algorithm used by Redis.

How to solve data loss with redis How to solve data loss with redis Apr 10, 2025 pm 08:24 PM

Redis data loss causes include memory failures, power outages, human errors, and hardware failures. The solutions are: 1. Store data to disk with RDB or AOF persistence; 2. Copy to multiple servers for high availability; 3. HA with Redis Sentinel or Redis Cluster; 4. Create snapshots to back up data; 5. Implement best practices such as persistence, replication, snapshots, monitoring, and security measures.

How to use the redis command line How to use the redis command line Apr 10, 2025 pm 10:18 PM

Use the Redis command line tool (redis-cli) to manage and operate Redis through the following steps: Connect to the server, specify the address and port. Send commands to the server using the command name and parameters. Use the HELP command to view help information for a specific command. Use the QUIT command to exit the command line tool.

See all articles