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

复习一下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));
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值