redis学习 jedis 类图及源码分析 博客分类: redis redis类图uml学习源码
redis 学习
查看 别人的类图 引用别人的链接:http://yychao.iteye.com/blog/1751583 可以对比一下,感觉不对,就自己画了一个
不对请指示(留言)
...........
Keyoperation
jedis.flushDB() 清空库中所有数据
Set<String> keys = jedis.keys("*"); 获取所有的key
jedis.del("key002") 系统中删除key002
jedis.exists("key002")) 判断key002是否存在
jedis.expire("key001", 5)设置 key001的过期时间为5秒
jedis.ttl("key001")) 查看key001的剩余生存时间
jedis.persist("key001"))移除key001的生存时间
jedis.type("key001")) 查看key所储存的值的类型
jedis.rename("key6", "key0")修改键名
jedis.move("foo", 1) 将当前db的key移动到给定的db当中
Stringoperation
jedis.set("key001", "value001") 赋key001值
jedis.get("key001") 取key001值
jedis.del("key001") 删除key001
jedis.append("key001", "+appendString") 在key002原来值后面追加
jedis.mset("key201", "value201", "key202", "value202", "key203", "value203", "key204", "value204"))
一次性新增key201,key202,key203,key204及其对应值
jedis.mget("key201", "key202", "key203", "key204"))
一次性获取key201,key202,key203,key204各自对应的值
jedis.del(new String[] { "key201", "key202" })
一次性删除key201,key202
listoperatoin
jedis.lpush("stringlists", "vector");
jedis.lpush("stringlists", "vector");
jedis.lpush("stringlists", "ArrayList"); 一次性添加list
jedis.lrange("stringlists", 0, -1) 所有元素-stringlists
jedis.lrem("stringlists", 2, "vector"))
删除列表指定的值 ,第二个参数为删除的个数(有重复时),后add进去的值先被删,类似于出栈
jedis.ltrim("stringlists", 0, 3)
删除下标0-3区间之外的元素
jedis.lpop("stringlists") 列表元素出栈
jedis.lset("stringlists", 0, "hello list!") 修改列表中指定下标的值
jedis.llen("stringlists") 长度-stringlists
/*
* list中存字符串时必须指定参数为alpha,如果不使用SortingParams,而是直接使用sort("list"),
* 会出现"ERR One or more scores can't be converted into double"
*/
SortingParams sortingParameters = new SortingParams();
sortingParameters.alpha();
sortingParameters.limit(0, 3);
jedis.sort("stringlists", sortingParameters)) 排序后的结果-stringlists
jedis.lindex("stringlists", 2) 获取下标为2的元素
SetOperate
jedis.sadd("sets", "element001"))
jedis.sadd("sets", "element002"))
jedis.sadd("sets", "element003")) 一次性添加set
jedis.smembers("sets")查看sets集合中的所有元素
jedis.srem("sets", "element003"))集合sets中删除元素element003
jedis.spop("sets")) sets集合中任意位置的元素出栈 出栈元素位置居然不定
jedis.sismember("sets", "element001")) 判断element001是否在集合sets中
Set<String> set = jedis.smembers("sets");
Iterator<String> it = set.iterator();
while (it.hasNext())
{Object obj = it.next();
System.out.println(obj);} 循环查询获取sets中的每个元素
jedis.sadd("sets1", "element001"))
jedis.sadd("sets1", "element002"))
jedis.sadd("sets1", "element003"))一次性添加set
jedis.sinter("sets1", "sets2"))sets1和sets2交集
jedis.sunion("sets1", "sets2"))sets1和sets2并集
jedis.sdiff("sets1", "sets2"))sets1和sets2差集
SortedSetOperate
jedis.zadd("zset", 7.0, "element001"))
jedis.zadd("zset", 8.0, "element002"))
jedis.zadd("zset", 2.0, "element003"))
jedis.zadd("zset", 3.0, "element004")) zset中添加元素
jedis.zrange("zset", 0, -1)) 按照权重值排序
jedis.zrem("zset", "element002")) zset中删除元素element002
jedis.zcard("zset")统计zset集合中的元素中个数
jedis.zscore("zset", "element004"))查看zset集合中element004的权重
jedis.zcount("zset", 1.0, 5.0)统计zset集合中权重某个范围内(1.0——5.0),元素的个数
jedis.zrange("zset", 1, 2))查看下标1到2范围内的元素值
HashOperate
jedis.hset("hashs", "key001", "value001")) hashs中添加key001和value001键值对
jedis.hset("hashs", "key002", "value002"))
jedis.hset("hashs", "key003", "value003"))
jedis.hincrBy("hashs", "key004", 4l))新增key004和4的整型键值对
jedis.hvals("hashs")hashs中的所有值
jedis.hdel("hashs", "key002"))hashs中删除key002键值对
jedis.hincrBy("hashs", "key004", 100))key004整型键值的值增加100
jedis.hexists("hashs", "key003"))判断key003是否存在
jedis.hget("hashs", "key004"))获取key004对应的值
jedis.hmget("hashs", "key001", "key003"))批量获取key001和key003对应的值
jedis.hkeys("hashs"))获取hashs中所有的key
jedis.hvals("hashs"))获取hashs中所有的value
以上 方法总结来自 http://www.cnblogs.com/edisonfeng/p/3571870.html
Jedis
jedis.set
public String set(final String key, String value) { checkIsInMulti(); client.set(key, value); return client.getStatusCodeReply(); }
jedis.get
public String get(final String key) { checkIsInMulti(); client.sendCommand(Protocol.Command.GET, key); return client.getBulkReply(); }
jedis.key("*")
public Set<String> keys(final String pattern) { checkIsInMulti(); client.keys(pattern); return BuilderFactory.STRING_SET .build(client.getBinaryMultiBulkReply()); }
jedis.mset() jedis.mget
public String mset(final String... keysvalues) { checkIsInMulti(); client.mset(keysvalues); return client.getStatusCodeReply(); }
public List<String> mget(final String... keys) { checkIsInMulti(); client.mget(keys); return client.getMultiBulkReply(); }
jedis.sadd
public void sadd(final byte[] key, final byte[]... members) { sendCommand(SADD, joinParameters(key, members)); }
private byte[][] joinParameters(byte[] first, byte[][] rest) { byte[][] result = new byte[rest.length + 1][]; result[0] = first; for (int i = 0; i < rest.length; i++) { result[i + 1] = rest[i]; } return result; }
jedis.smembers
public Set<String> smembers(final String key) { checkIsInMulti(); client.smembers(key); final List<String> members = client.getMultiBulkReply(); return new HashSet<String>(members); }
public static final Builder<List<String>> STRING_LIST = new Builder<List<String>>() { @SuppressWarnings("unchecked") public List<String> build(Object data) { if (null == data) { return null; } List<byte[]> l = (List<byte[]>) data; final ArrayList<String> result = new ArrayList<String>(l.size()); for (final byte[] barray : l) { if (barray == null) { result.add(null); } else { result.add(SafeEncoder.encode(barray)); } } return result; } public String toString() { return "List<String>"; } };
jedis.hset
public Long hset(String key, String field, String value) { Jedis j = getShard(key); return j.hset(key, field, value); }
jedis.hget
public String hget(String key, String field) { Jedis j = getShard(key); return j.hget(key, field); }
private final Hashing algo; private final Map<ShardInfo<R>, R> resources = new LinkedHashMap<ShardInfo<R>, R>();
public R getShard(String key) { return resources.get(getShardInfo(key)); }
public S getShardInfo(byte[] key) { SortedMap<Long, S> tail = nodes.tailMap(algo.hash(key)); if (tail.size() == 0) { return nodes.get(nodes.firstKey()); } return tail.get(tail.firstKey()); }
Connection
Connection.connect()
public void connect() { if (!isConnected()) { try { socket = new Socket(); //->@wjw_add socket.setReuseAddress(true); socket.setKeepAlive(true); //Will monitor the TCP connection is valid socket.setTcpNoDelay(true); //Socket buffer Whetherclosed, to ensure timely delivery of data socket.setSoLinger(true,0); //Control calls close () method, the underlying socket is closed immediately //<-@wjw_add socket.connect(new InetSocketAddress(host, port), timeout); socket.setSoTimeout(timeout); outputStream = new RedisOutputStream(socket.getOutputStream()); inputStream = new RedisInputStream(socket.getInputStream()); } catch (IOException ex) { throw new JedisConnectionException(ex); } } }
Connection.getStatusCodeReply
protected String getStatusCodeReply() { flush(); pipelinedCommands--; final byte[] resp = (byte[]) Protocol.read(inputStream); if (null == resp) { return null; } else { return SafeEncoder.encode(resp); } }
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public Set<String> sdiff(final String... keys) { checkIsInMulti(); client.sdiff(keys); return BuilderFactory.STRING_SET .build(client.getBinaryMultiBulkReply()); }client.sdiff
public void sdiff(final String... keys) { final byte[][] bkeys = new byte[keys.length][]; for (int i = 0; i < bkeys.length; i++) { bkeys[i] = SafeEncoder.encode(keys[i]); } sdiff(bkeys); }binaryclient.sdiff
public void sdiff(final byte[]... keys) { sendCommand(SDIFF, keys); }connection.sendcommand
protected Connection sendCommand(final Command cmd, final byte[]... args) { connect(); Protocol.sendCommand(outputStream, cmd, args); pipelinedCommands++; return this; }protocol.sendcommand
public static void sendCommand(final RedisOutputStream os,
final Command command, final byte[]... args) {
sendCommand(os, command.raw, args);
}
private static void sendCommand(final RedisOutputStream os, final byte[] command, final byte[]... args) { try { os.write(ASTERISK_BYTE); os.writeIntCrLf(args.length + 1); os.write(DOLLAR_BYTE); os.writeIntCrLf(command.length); os.write(command); os.writeCrLf(); for (final byte[] arg : args) { os.write(DOLLAR_BYTE); os.writeIntCrLf(arg.length); os.write(arg); os.writeCrLf(); } } catch (IOException e) { throw new JedisConnectionException(e); } }buildfactory.STRING_SET
public static final Builder<Set<String>> STRING_SET = new Builder<Set<String>>() { @SuppressWarnings("unchecked") public Set<String> build(Object data) { if (null == data) { return null; } List<byte[]> l = (List<byte[]>) data; final Set<String> result = new HashSet<String>(l.size()); for (final byte[] barray : l) { if (barray == null) { result.add(null); } else { result.add(SafeEncoder.encode(barray)); } } return result; } public String toString() { return "Set<String>"; } };
public List<byte[]> getBinaryMultiBulkReply() { flush(); pipelinedCommands--; return (List<byte[]>) Protocol.read(inputStream); }protocol.read
public static Object read(final RedisInputStream is) { return process(is); }
private static Object process(final RedisInputStream is) { try { byte b = is.readByte(); if (b == MINUS_BYTE) { processError(is); } else if (b == ASTERISK_BYTE) { return processMultiBulkReply(is); } else if (b == COLON_BYTE) { return processInteger(is); } else if (b == DOLLAR_BYTE) { return processBulkReply(is); } else if (b == PLUS_BYTE) { return processStatusCodeReply(is); } else { throw new JedisConnectionException("Unknown reply: " + (char) b); } } catch (IOException e) { throw new JedisConnectionException(e); } return null; }以上的可以看出 : jedis.sdiff("sets1", "sets2") sets1和sets2差集 差集:set1中有,set2中没有的元素
捐助开发者
在兴趣的驱动下,写一个免费
的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。
谢谢您的赞助,我会做的更好!