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();
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章