关闭

redis中的数据结构基本的操作

297人阅读 评论(0) 收藏 举报
分类:

复习一下redis中的各个数据结构的基本操作

当然后首先要引入redis的包

package com.jedis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;


public class JedisUtil {
private JedisPool jedisPool;
private Jedis jedis;

public JedisUtil() {
jedisPool = new JedisPool(new JedisPoolConfig(), "127.0.0.1");
jedis = jedisPool.getResource();
}

public Jedis getJedis() {
return this.jedis;
}


public static void main(String[] args) {
// manipulate String crud
JedisUtil jedisUtil = new JedisUtil();
Jedis jedis = jedisUtil.getJedis();
jedis.set("hello", "world");
System.out.println(jedis.get("hello"));
jedis.append("hello", " hello world!");
System.out.println(jedis.get("hello"));
jedis.set("hello", "hello");
System.out.println(jedis.get("hello"));
jedis.del("hello");
System.out.println(jedis.get("hello"));


// manpulate map
Map<String, String> map = new HashMap<String, String>();
map.put("a", "1");
map.put("b", "2");
map.put("c", "3");
jedis.hmset("user", map);
System.out.println(jedis.hmget("user", "a", "b", "c"));
// the result is generic type list
List<String> list = jedis.hmget("user", "a", "b", "c");
System.out.println(list.size());
// delet a key-value
jedis.hdel("user", "a");
System.out.println(jedis.hmget("user", "a"));// null
System.out.println(jedis.hlen("user"));// 2
System.out.println(jedis.exists("user"));// true
System.out.println(jedis.hkeys("user"));
Set<String> keys = jedis.hkeys("user");
for (String string : keys) {
System.out.println(string);
}
System.out.println(jedis.hvals("user"));
List<String> vals = jedis.hvals("user");
for (String string : vals) {
System.out.println(string);
}


// manipulate list
jedis.del("fruits");
System.out.println(jedis.lrange("fruits", 0, -1));
jedis.lpush("fruits", "apple");
jedis.lpush("fruits", "banana");
jedis.lpush("fruits", "orange");
System.out.println(jedis.lrange("fruits", 0, -1));
// manipulate set
jedis.sadd("sname", "jack", "tom", "rose");
System.out.println(jedis.smembers("sname"));
Set<String> smembers = jedis.smembers("sname");// return a set
// remove a value
// jedis.srem("sname", "jack");
System.out.println(jedis.smembers("sname"));
// value is a member of set
System.out.println(jedis.sismember("sname", "jack"));// false
// return a random element from a Set
System.out.println(jedis.srandmember("sname"));
// return the set cardinality(number of the elements)
System.out.println(jedis.scard("sname"));


// others
System.out.println(jedis.keys("*"));
System.out.println(jedis.keys("*name"));
// System.out.println(jedis.del("sname"));
System.out.println(jedis.del("ssname"));
/*
* The TTL command returns the remaining time to live in seconds of a
* key that has an EXPIRE set. This introspection capability allows a
* Redis client to check how many seconds a given key will continue to
* be part of the dataset.
* Integer reply, returns the remaining time to
* live in seconds of a key that has an EXPIRE. If the Key does not
* exists or does not have an associated expire, -1 is returned
*/
System.out.println(jedis.ttl("sname"));
jedis.setex("timekey", 8, "min");//set the expirtation of the key
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(jedis.ttl("timekey"));//3
jedis.setex("timekey", 1, "min");
System.out.println(jedis.ttl("timekey"));
System.out.println(jedis.exists("time"));
System.out.println(jedis.rename("timekey", "time"));
System.out.println(jedis.get("timekey"));
System.out.println(jedis.get("time"));
//jedis sort
jedis.del("arr");
jedis.rpush("arr", "3");
jedis.lpush("arr", "2");
jedis.lpush("arr", "4");
jedis.lpush("arr", "1");
System.out.println(jedis.lrange("arr", 0, -1));
System.out.println(jedis.sort("arr"));
System.out.println(jedis.lrange("arr", 0, -1));
}
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:127577次
    • 积分:2333
    • 等级:
    • 排名:第16760名
    • 原创:101篇
    • 转载:33篇
    • 译文:0篇
    • 评论:15条
    文章分类
    最新评论