Jedis(二)——String

Key值通配符

既然是数据库,肯定有模糊查询的需求,Redis允许使用通配符,这一点,在做一些批量删除的时候,会非常实用

*: 通配任意多个字符

?: 通配单个字符

[]: 通配括号内的某1个字符

String类型

因为存在一种技术,叫序列化,只要是数据,都能转化成字符串,图省事的话,只使用这一种,就够用了

package cn.swsk.xbry.api.v1.md1001;

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

/**
 * @author css
 * @date 2019/9/24 22:20
 */
public class Test {
   public static void main(String[] args) throws InterruptedException {
      JedisPoolConfig config = new JedisPoolConfig();
      config.setMaxTotal(25);
      config.setMaxIdle(20);
      config.setMinIdle(5);
      try (
         JedisPool pool = new JedisPool(config, "127.0.0.1", 6379);
         Jedis jedis = pool.getResource()) {
         jedis.select(0);
         //type of String

         String key1 = "key1", key2 = "key2";
         if (!jedis.exists(key1)) {
            jedis.set(key1, "test");
         }
         //设置key生存时间,当key过期时,它会被自动删除。 Time为秒数
         jedis.expire(key1, 1);

         //Setex 命令为指定的 key 设置值及其过期时间。如果 key 已经存在, SETEX 命令将会替换旧的值。
         jedis.setex(key1, 20, "1312312");

         //查看key的生存时间的剩余时间,若为-1,说明key已过期
         System.out.println(jedis.ttl(key1));

         System.out.println(jedis.get(key1));
         Thread.sleep(1200);
         System.out.println(jedis.get(key1));

         try {
            //key1会被直接覆盖, key2不存在时报错
            jedis.rename(key2, key1);
         } catch (JedisDataException e) {
            e.printStackTrace();
            //TODO: no such key
         }

      }
   }
}
原文地址:https://www.cnblogs.com/chenss15060100790/p/11604391.html