代码:
https://github.com/viakiba/redis
主要是:
MIGRATE,MOVE,OBJECT,PERSIST/PEXPIRE/PEXPIREAT,PTTL/TTL
1import org.junit.Test; 2 3import redis.clients.jedis.Jedis; 4 5/** 6 * @description: 7 * 关于key的相关操作(2) 8 * MIGRATE,MOVE,OBJECT,PERSIST/PEXPIRE/PEXPIREAT,PTTL/TTL 9 * 10 * 参考:http://redisdoc.com/index.html 11 * @author viakiba 12 * @date 2017年8月17日 13 */ 14public class App3 { 15 static interface RedisConfig { 16 public final static String HOST = "127.0.0.1"; 17 public final static int PORT = 6379; 18 } 19 20 /** 21 * @description: 测试连接是否通过 22 * @author: viakiba 23 * @throws Exception 24 */ 25 @Test 26 public void test1() throws Exception { 27 //连接测试是否通过 28 Jedis jedis = new Jedis(RedisConfig.HOST,RedisConfig.PORT); 29 System.out.println(jedis.ping());//pong则通过 30 } 31 32 /** 33 * @description: MIGRATE 34 * 将 key 原子性地从当前实例传送到目标实例的指定数据库上,一旦传送成功, 35 * key 保证会出现在目标实例上,而当前实例上的 key 会被删除。 36 * 注意: 37 * 这个命令是一个原子操作,它在执行的时候会阻塞进行迁移的两个实例, 38 * 直到以下任意结果发生:迁移成功,迁移失败,等待超时。 39 * 参考: 40 * http://redisdoc.com/key/migrate.html 41 * @author: viakiba 42 * @throws Exception 43 */ 44 @Test 45 public void test2() throws Exception { 46 //jedis不存在该命令 47 Jedis jedis = new Jedis(RedisConfig.HOST,RedisConfig.PORT); 48 jedis.flushAll(); 49 } 50 51 /** 52 * @description: MOVE 53 * 将当前数据库的 key 移动到给定的数据库 db 当中。 54 * 注意: 55 * 如果当前数据库(源数据库)和给定数据库(目标数据库)有相同名字的给定 key , 56 * 或者 key 不存在于当前数据库,那么 MOVE 没有任何效果。 57 * 因此,也可以利用这一特性,将 MOVE 当作锁(locking)原语(primitive)。 58 * @author: viakiba 59 * @throws Exception 60 */ 61 @Test 62 public void test3() throws Exception { 63 Jedis jedis = new Jedis(RedisConfig.HOST,RedisConfig.PORT); 64 jedis.flushAll();//清空所有数据 65 System.out.println("=====准备数据========="); 66 jedis.set("k1", "v1");//默认选择index为0的数据库 67 System.out.println("index0===>"+jedis.get("k1")); 68 jedis.select(1); 69 System.out.println("index1===>"+jedis.get("k1")); 70 System.out.println("======执行move======"); 71 jedis.select(0); 72 jedis.move("k1", 1); 73 System.out.println("======查看结果======"); 74 System.out.println("index0===>"+jedis.get("k1")); 75 jedis.select(1); 76 System.out.println("index1===>"+jedis.get("k1")); 77 } 78 79 /** 80 * @description: OBJECT 81 * OBJECT 命令允许从内部察看给定 key 的 Redis 对象。 82 * @author: viakiba 83 * @throws Exception 84 */ 85 @Test 86 public void test4() throws Exception { 87 Jedis jedis = new Jedis(RedisConfig.HOST,RedisConfig.PORT); 88// jedis.flushAll();//清空所有数据 89// System.out.println("=====准备数据========="); 90// jedis.set("k1", "v1");//默认选择index为0的数据库 91 92 //OBJECT REFCOUNT <key> 返回给定 key 引用所储存的值的次数。此命令主要用于除错 93// Long objectRefcount = jedis.objectRefcount("k1"); 94// System.out.println(objectRefcount); 95 //OBJECT ENCODING <key> 返回给定 key 锁储存的值所使用的内部表示(representation)。 96 //个人理解 编码方式 embstr 这与value有关 编码有多种 97 /* 98 字符串可以被编码为 raw (一般字符串)或 int (为了节约内存,Redis 会将字符串表示的 64 位有符号整数编码为整数来进行储存)。 99 列表可以被编码为 ziplist 或 linkedlist 。 ziplist 是为节约大小较小的列表空间而作的特殊表示。 100 集合可以被编码为 intset 或者 hashtable 。 intset 是只储存数字的小集合的特殊表示。 101 哈希表可以编码为 zipmap 或者 hashtable 。 zipmap 是小哈希表的特殊表示。 102 有序集合可以被编码为 ziplist 或者 skiplist 格式。 ziplist 用于表示小的有序集合,而 skiplist 则用于表示任何大小的有序集合。 103 */ 104// String objectEncoding = jedis.objectEncoding("k1"); 105// System.out.println(objectEncoding); 106 //OBJECT IDLETIME <key> 返回给定 key 自储存以来的空闲时间(idle, 没有被读取也没有被写入),以秒为单位。 107 Long objectIdletime = jedis.objectIdletime("k1"); 108 System.out.println(objectIdletime);//单位是秒 109 } 110 111 /** 112 * @description: PTTL/TTL 113 * TTL:以秒为单位,返回给定 key 的剩余生存时间(TTL, time to live)。 114 * PTTL:这个命令类似于 TTL 命令,但它以毫秒为单位返回 key 的剩余生存时间,而不是像 TTL 命令那样,以秒为单位。 115 * @author: viakiba 116 * @throws Exception 117 */ 118 @Test 119 public void test5() throws Exception { 120 Jedis jedis = new Jedis(RedisConfig.HOST,RedisConfig.PORT); 121 jedis.flushAll();//清空所有数据 122 System.out.println("=====准备数据========="); 123 jedis.set("k0", "v0");//默认选择index为0的数据库 124 jedis.setex("k1", 5, "v1");//默认选择index为0的数据库 125 Thread.sleep(3000); 126 /** 127 当 key 不存在时,返回 -2 。 128 当 key 存在但没有设置剩余生存时间时,返回 -1 。 129 否则,以秒为单位,返回 key 的剩余生存时间。 130 */ 131 System.out.println(jedis.ttl("k2"));//秒级,另外pttl()jedis没有提供 132 System.out.println(jedis.ttl("k0"));//秒级,另外pttl()jedis没有提供 133 System.out.println(jedis.ttl("k1"));//秒级,另外pttl()jedis没有提供 134 } 135 136 /** 137 * @description: PEXPIRE/PEXPIREAT/PERSIST 138 * PERSIST:移除给定 key 的生存时间,将这个 key 从『易失的』(带生存时间 key )转换成『持久的』(一个不带生存时间、永不过期的 key )。 139 * PEXPIREAT:这个命令和 EXPIREAT 命令类似,但它以毫秒为单位设置 key 的过期 unix 时间戳,而不是像 EXPIREAT 那样,以秒为单位。 140 * PEXPIRE:这个命令和 EXPIRE 命令的作用类似,但是它以毫秒为单位设置 key 的生存时间,而不像 EXPIRE 命令那样,以秒为单位。 141 * @author: viakiba 142 * @throws Exception 143 */ 144 @Test 145 public void test6() throws Exception { 146// Jedis jedis = new Jedis(RedisConfig.HOST,RedisConfig.PORT); 147// jedis.flushAll();//清空所有数据 148// System.out.println("=====准备数据========="); 149// jedis.setex("k0", 10, "v0");//默认选择index为0的数据库 150// System.out.println(jedis.ttl("k0")); 151// jedis.persist("k0"); 152// System.out.println("==========="); 153// System.out.println(jedis.ttl("k0")); 154 155 156 } 157}