import java.util.List; import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.connection.RedisListCommands.Position; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; public class TestRedisList { private static JedisConnectionFactory getJedisConnectionFactory() { JedisConnectionFactory jcf = new JedisConnectionFactory(); jcf.setHostName("127.0.0.1"); jcf.setPort(6379); jcf.setPassword("123"); jcf.afterPropertiesSet(); return jcf; } private static RedisConnection getRedisConnection() { return getJedisConnectionFactory().getConnection(); } public static void main(String[] args) { RedisConnection rc = getRedisConnection(); // rc.del("userList".getBytes()); //LPUSH long lPush = rc.lPush("userList".getBytes(), "Jim".getBytes(), "Tom".getBytes(), "Jack".getBytes(), "Scott".getBytes(), "Rob".getBytes()); System.out.println("LPUSH [" + lPush + "]"); //LPUSHX long lPushX = rc.lPushX("noexists".getBytes(), "test".getBytes()); System.out.println("LPUSHX [" + lPushX + "]"); //LRANGE List<byte[]> lRange = rc.lRange("userList".getBytes(), 0, -1); for (byte[] value: lRange) { System.out.println(new String(value)); } //LPOP byte[] lPop = rc.lPop("userList".getBytes()); System.out.println("LPOP [" + new String(lPop) + "]"); //BLPOP List<byte[]> blPop = rc.bLPop(0, "userList".getBytes()); for (byte[] value : blPop) { System.out.println("BLPOP [" + new String(value) + "]"); } //如果 deptList 列表为空,则阻塞,直到等待超时或发现可弹出元素为止。 // List<byte[]> deptList = rc.bLPop(11110, "deptList".getBytes()); // for (byte[] value : deptList) { // System.out.println("deptList [" + new String(value) + "]"); // } // // List<byte[]> twoList = rc.bLPop(11110, "tList".getBytes(), "deptList".getBytes()); // for (byte[] value : twoList) { // System.out.println("twoList [" + new String(value) + "]"); // } //RPOP byte[] rPop = rc.rPop("userList".getBytes()); System.out.println("RPOP [" + new String(rPop) + "]"); //BRPOP List<byte[]> brPop = rc.bRPop(0, "userList".getBytes()); for (byte[] value : brPop) { System.out.println("BRPOP [" + new String(value) + "]"); } //RPOPLPUSH byte[] rPopPlPush = rc.rPopLPush("userList".getBytes(), "userList".getBytes()); System.out.println("RPOPLPUSH [" + new String(rPopPlPush) + "]"); // byte[] twoList = rc.rPopLPush("alist".getBytes(), "blist".getBytes()); // System.out.println("twoList [" + new String(twoList) + "]"); //LINDEX byte[] lIndex = rc.lIndex("userList".getBytes(), 0); System.out.println("LINDEX [" + new String(lIndex) + "]"); //LINSERT long lInsert = rc.lInsert("userList".getBytes(), Position.AFTER, "Jack".getBytes(), "Green".getBytes()); System.out.println("LINSERT [" + lInsert + "]"); //LLEN long lLen = rc.lLen("userList".getBytes()); System.out.println("LLEN ["+ lLen + "]"); //LREM long lRem = rc.lRem("userList".getBytes(), 1, "Jack".getBytes()); System.out.println("LREM [" + lRem + "]"); //LSET rc.lSet("userList".getBytes(), 1, "newValue".getBytes()); //LTRIM rc.lTrim("userList".getBytes(), 1, -1); //RPUSH 将一个或多个值 value 插入到列表 key 的表尾(最右边) long rPush = rc.rPush("newlist".getBytes(), "Jim".getBytes(), "Green".getBytes()); System.out.println("RPUSH [" + rPush + "]"); //RPUSHX 将值 value 插入到列表 key 的表尾,当且仅当 key 存在并且是一个列表。 long rPushX = rc.rPushX("newlistb".getBytes(), "Tom".getBytes()); System.out.println("RPUSHX [" + rPushX + "]"); //BRPOPLPUSH byte[] bRPopLpush = rc.bRPopLPush(1000, "newlist".getBytes(), "newlistb".getBytes()); System.out.println("BRPOPLPUSH [" + new String(bRPopLpush) + "]"); } }
学习 Redis List(列表)
猜你喜欢
转载自injavawetrust.iteye.com/blog/2296046
今日推荐
周排行