redis常用命令

package com.knight.redissample;

import java.util.List;
import java.util.Set;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Response;
import redis.clients.jedis.Transaction;

/**
   Redis入门指南 第2版
   Redis in action
 */
public class App {
	public static void main(String[] args) {
		Jedis jedis = new Jedis("localhost");
/** 
chapter 3.2 字符串类型 strings-------------------------------------------------------------
		
	Table 1.3 Commands used on STRING values
	  Commond |what it does
        GET     |Fetches the data stored at the given key
        SET     |Sets the value stored at the given key
        DEL     |Deletes the value stored at the given key (works for all types)
		
	Table 3.1 Increment and decrement commands in Redis
	  Commond    	  |Example use and description
	    INCR     	|INCR key-name—Increments the value stored at the key by 1
        DECR    	|DECR key-name—Decrements the value stored at the key by 1
        INCRBY  	|INCRBY key-name amount—Increments the value stored atthe 
        		    key by the provided integer value	 
        DECRBY  	|DECRBY key-name amount—Decrements the value stored atthe 
                            key by the provided integer value         
        INCRBYFLOAT     |INCRBYFLOAT key-name amount—Increments the valuestored
                            at the key by the provided float value (available in Redis 2.6and later)

		常用命令:
		 append key value 向尾部增加值
		 strlen key 获取字符串长度
		 MGET MSET 同时设置多个键
		 getbit setbit bitcount bitop 位操作
		 
	          例子:
			$ redis-cli
			redis > set hello world
			OK
			redis > get hello
			"world"
			redis > del hello
			(integer) 1
			redis > get hello
			(nil)
			redis >

chapter 3.4 列表类型lists-------------------------------------------------------------
		Table 1.4 Commands used on LIST values
		Command |What it does
		RPUSH   |Pushes the value onto the right end of the list
		LRANGE  |Fetches a range of values from the list
		LINDEX  |Fetches an item at a given position in the list
		LPOP    |Pops the value from the left end of the list and returns it

		Table 3.3 Some commonly used LIST commands
		Command Example use and description
		RPUSH 	|RPUSH key-name value [value ...]—Pushes the value(s) onto the right end of the list
		LPUSH 	|LPUSH key-name value [value ...]—Pushes the value(s) onto the left end of the list
		RPOP 	|RPOP key-name—Removes and returns the rightmost item from the list
		LPOP 	|LPOP key-name—Removes and returns the leftmost item from the list
		LINDEX 	|LINDEX key-name offset—Returns the item at the given offset
		LRANGE 	|LRANGE key-name start end—Returns the items in the list at the offsets from start to
				 end, inclusive
		LTRIM 	|LTRIM key-name start end—Trims the list to only include items at indices between
				 start and end, inclusive
		常用命令
		 llen 获取列表中元素的个数,当键不存在时会返回0
		 lange 获取列表片段:lange numbers 0 2
		 lrem 删除列表中指定的值
		 lindex 获取指定索引值 :lindex key index
		 lset 设置指定索引值: key index value
		 ltrim 限制列表中元素数量 ltrim log 0 99
		 linsert key before|after pivot value 向列表中插入元素
		 rpoplpush source destination 将元素从一个列表转到另一个列表

	         例子:
		  Redis入门指南 第2版
		  1.
		  redis> lpush numbers 1
		  (interger) 1
		  redis> lpush numbers 1 2
		  (interger) 3
		  redis> rpush numbers 0 -1
		  (interger) 5
		  numbers中键的数据   [3,2,1,0,-1]
		  2.从列表两端弹出元素
		  lpop key
		  rpop key
		  例子:
		  redis> lpop numbers
		  "3"
		  redis> rpop numbers
		  "-1"
		  numbers中键的数据   [2,1,0]
		  4.获取列表片段
		  lrange key start stop
		  lrange命令支持负索引,表示从右边开始计数,如:"-1"表示最右边第一个元素,"-2"表示最右边第二个元素
		  例子:
		  redis> lrange numbers 0 2
		  1)"2"
		  2)"1"
		  3)"0"
		  redis> lrange numbers -2 -1
		  1)"1"
		  2)"0"
		  		
chapter 3.3散列类型Hashes-------------------------------------------------------------
		Table 3.7 Operations for adding and removing items from HASHes
		Command |Example use and description
		HMGET 	|HMGET key-name key [key ...]—Fetches the values at the fields in the HASH
		HMSET 	|HMSET key-name key value [key value ...]—Sets the values of the
				 fields in the HASH
		HDEL 	|HDEL key-name key [key ...]—Deletes the key-value pairs in the HASH,
				 returning the number of pairs that were found and deleted
		HLEN 	|HLEN key-name—Returns the number of key-value pairs in the HASH


		Table 3.8 More bulk operations and STRING-like calls over HASHes
		Command      |Example use and description
		HEXISTS 	 |HEXISTS key-name key—Returns whether the given key exists in the HASH
		HKEYS 		 |HKEYS key-name—Fetches the keys in the HASH
		HVALS 		 |HVALS key-name—Fetches the values in the HASH
		HGETALL 	 |HGETALL key-name—Fetches all key-value pairs from the HASH
		HINCRBY 	 |HINCRBY key-name key increment—Increments the value stored at the
				      given key by the integer increment
		HINCRBYFLOAT |HINCRBYFLOAT key-name key increment—
		
		常用命令
		 hmset hmget hgetall
		 hsetnx 当字段不存在时赋值
		 hkeys hvals 只获取字段名或字段值
		 例子:
		 redis 127.0.0.1:6379> hset hash-key sub-key1 value1
		(integer) 1
		redis 127.0.0.1:6379> hset hash-key sub-key2 value2
		(integer) 1
		redis 127.0.0.1:6379> hset hash-key sub-key1 value1
		(integer) 0
		redis 127.0.0.1:6379> hgetall hash-key
		1) "sub-key1"
		2) "value1"
		3) "sub-key2"
		4) "value2"
		redis 127.0.0.1:6379> hdel hash-key sub-key2
		(integer) 1
		redis 127.0.0.1:6379> hdel hash-key sub-key2
		(integer) 0
		redis 127.0.0.1:6379> hget hash-key sub-key1
		"value1"
		redis 127.0.0.1:6379> hgetall hash-key
		1) "sub-key1"
		2) "value1"

chapter 3.5集合类型sets-------------------------------------------------------------
		Table 1.5 Commands used on SET values
		Command 	|What it does
		SADD 		|Adds the item to the set
		SMEMBERS 	|Returns the entire set of items
		SISMEMBER 	|Checks if an item is in the set
		SREM 		|Removes the item from the set, if it exists

		
		Table 3.5 Some commonly used SET commands
		Command  	|Example use and description
		SADD 		|SADD key-name item [item ...]—Adds the items to the set and returns 
		             the number of items added that weren’t already present 
		SREM 		|SREM key-name item [item ...]—Removes the items and returns the
		             number of items that were removed      
		SISMEMBER 	|SISMEMBER key-name item—Returns whether the item is in the SET
		SCARD 		|SCARD key-name—Returns the number of items in the SET
		SMEMBERS	|SMEMBERS key-name—Returns all of the items in the SET as a Python set
		SRANDMEMBER |SRANDMEMBER key-name [count]—Returns one or more random items
					 from the SET. When count is positive, Redis will return count distinct randomly chosen
					 items, and when count is negative, Redis will return count randomly chosen
					 items that may not be distinct.
		SPOP        |SPOP key-name—Removes and returns a random item from the SET
		SMOVE       |SMOVE source-key dest-key item—If the item is in the source, removes
                     the item from	
        
        Table 3.6 Operations for combining and manipulating SETs in Redis
		Command Example use and description
		SDIFF 		|SDIFF key-name [key-name ...]—Returns the items in the first SET
				     that weren’t in any of the other SETs (mathematical set difference operation)
		SDIFFSTORE  |SDIFFSTORE dest-key key-name [key-name ...]—Stores at the
					 dest-key the items in the first SET that weren’t in any of the other SETs (mathematical
		             set difference operation)
		SINTER 		|SINTER key-name [key-name ...]—Returns the items that are in all of
		             the SETs (mathematical set intersection operation)
		SINTERSTORE |SINTERSTORE dest-key key-name [key-name ...]—Stores at the
					 dest-key the items that are in all of the SETs (mathematical set intersection
		             operation)
		SUNION 		|SUNION key-name [key-name ...]—Returns the items that are in at
					 least one of the SETs (mathematical set union operation)
		SUNIONSTORE |SUNIONSTORE dest-key key-name [key-name ...]—Stores at the
					 dest-key the items that are in at least one of the SETs (mathematical set union
					 operation)		             
		                        
		常用命令:
		 sadd letters a b c
		 srem letter c d
		 smembers key 获取结合中所有元素
		 sismember letter a 判断集合是否存在
		  集合间运算
		 sdiff 差集运算 sdiff seta setb
		 sinter 交集运算
		 sunion 并集运算

chapter 3.6有序集合类型sorted sets-------------------------------------------------------------
		Table 1.7 Commands used on ZSET values
		Command 		|What it does
		ZADD    		|Adds member with the given score to the ZSET
		ZRANGE  		|Fetches the items in the ZSET from their positions in sorted order
		ZRANGEBYSCORE 	|Fetches items in the ZSET based on a range of scores
		ZREM 			|Removes the item from the ZSET, if it exists

		Table 3.9 Some common ZSET commands
		Command 	|Example use and description
		ZADD 		|ZADD key-name score member [score member ...]—Adds members with
					 the given scores to the ZSET
		ZREM 		|ZREM key-name member [member ...]—Removes the members from the
					 ZSET, returning the number of members that were removed
		ZCARD 		|ZCARD key-name—Returns the number of members in the ZSET
		ZINCRBY 	|ZINCRBY key-name increment member—Increments the member in the ZSET
		ZCOUNT 	    |ZCOUNT key-name min max—Returns the number of members with scores
					 between the provided minimum and maximum
		ZRANK 		|ZRANK key-name member—Returns the position of the given member in the ZSET
		ZSCORE 		|ZSCORE key-name member—Returns the score of the member in the ZSET
		ZRANGE 		|ZRANGE key-name start stop [WITHSCORES]—Returns the members and
					 optionally the scores for the members with ranks between start and stop	
		
		常用命令
		 zrevrange
		 zrangebyscore
		 zincrby 增加某个元素的分数

------------------------------------------------------------------------------------------------
		// chapter4.1.3 p71 watch命令实现自增长
		// expire key second 单位是秒
		// ttl 查看一个键还有多久时间过期

		// chapter 4.3 排序
		// sort支持对列表和有序集合进行排序

		// 事物
		// Transaction multi = jedis.multi();
		// multi.exec();
		//
		// //管道
		// Pipeline pip = jedis.pipelined();
		// pip.set("foo", "bar");
		// pip.get("foo");
		// Response<List<Object>> result = pip.exec();

		// 任务队列 brpop

		// 排序

		// Jedis Cluster
		// Redis cluster specification (still under development) is implemented
		// Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
		 Jedis Cluster will attempt to discover cluster nodes automatically
		// jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7379));
		// JedisCluster jc = new JedisCluster(jedisClusterNodes);
		// jc.set("foo", "bar");
		// String value = jc.get("foo");
		
		
		**/
		jedis.close();
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值