Redis是一個開源的使用ANSI C語言編寫、支持網絡、可基於內存亦可持久化的日誌型、Key-Value數據庫,並提供多種語言的API。從2010年3月15日起,Redis的開發工做由VMware主持。redis
若是真要把Redis與Memcached進行對比,參考下圖:
使用Memcached,讓我感觸頗深的是Object Size的問題,因爲SQL未做優化直接映射對象,致使緩存對象大於1MB,Memcached就拋了異常。而Redis默認緩存對象512MB,最大支持1GB。至少在緩存對象時,能夠有更大的伸縮空間了! 此外,是數據類型。Memcached比較簡單,而Redis能夠支持更多複雜的數據類型,如HASH、SET、SortedSet等等。數據庫
2、安裝使用 緩存
一、安裝操做命令 網絡
port install redis併發
二、運行redis命令app
redis-server /opt/local/etc/redis.confmemcached
三、查看是否按照成功、使用使用緩存操做。oop
Holele-Book-Pro:~ user$ redis-cli redis 127.0.0.1:6379> ping PONG redis 127.0.0.1:6379> set mykey somevalue OK redis 127.0.0.1:6379> get mykey "somevalue" redis 127.0.0.1:6379> del mykey (integer) 1 redis 127.0.0.1:6379> get mykey (nil) redis 127.0.0.1:6379>
3、Redis經常使用操做使用命令。 性能
一、append key value 測試
redis 127.0.0.1:6379> EXISTS mykey (integer) 0 redis 127.0.0.1:6379> append mykey "www.sdgou.com" (integer) 13 redis 127.0.0.1:6379> append mykey " welcome" (integer) 21 redis 127.0.0.1:6379> get mykey "www.sdgou.com welcome" redis 127.0.0.1:6379>
二、bitcount key [start] [end]
redis 127.0.0.1:6379> del mykey (integer) 1 redis 127.0.0.1:6379> get mykey (nil) redis 127.0.0.1:6379> set mykey "http://www.sdgou.com" OK redis 127.0.0.1:6379> bitcount mykey (integer) 93 redis 127.0.0.1:6379> bitcount mykey 0 0 (integer) 3 redis 127.0.0.1:6379> bitcount mykey 1 1 (integer) 4 redis 127.0.0.1:6379>
三、bitop operation destkey key [key...]
位運算,按位與、按位或、按位異或、按位非
redis 127.0.0.1:6379> set mykey1 1 OK redis 127.0.0.1:6379> set mykey2 1 OK redis 127.0.0.1:6379> bitop and myresult mykey1 mykey2 (integer) 1 redis 127.0.0.1:6379> get myresult "1" redis 127.0.0.1:6379> set mykey2 0 OK redis 127.0.0.1:6379> bitop and myresult mykey1 mykey2 (integer) 1 redis 127.0.0.1:6379> get myresult "0" redis 127.0.0.1:6379> bitop or myresult mykey1 mykey2 (integer) 1 redis 127.0.0.1:6379> get myresult "1" redis 127.0.0.1:6379> set mykey1 0 OK redis 127.0.0.1:6379> bitop or myresult mykey1 mykey2 (integer) 1 redis 127.0.0.1:6379> get myresult "0"
四、decr key value
redis 127.0.0.1:6379> set key1 "10" OK redis 127.0.0.1:6379> redis 127.0.0.1:6379> decr key1 (integer) 9 redis 127.0.0.1:6379> get key1 "9" redis 127.0.0.1:6379> set key1 "12312434325325254364654745" OK redis 127.0.0.1:6379> decr key1 (error) ERR value is not an integer or out of range redis 127.0.0.1:6379> redis 127.0.0.1:6379> get key1 "12312434325325254364654745" redis 127.0.0.1:6379> redis 127.0.0.1:6379> del key1 (integer) 1 redis 127.0.0.1:6379> set key1 "10" OK redis 127.0.0.1:6379> get key1 "10" redis 127.0.0.1:6379> decr key1 (integer) 9 redis 127.0.0.1:6379> get key1 "9" redis 127.0.0.1:6379> decr key1 (integer) 8 redis 127.0.0.1:6379> get key1 "8" redis 127.0.0.1:6379>
五、decrby key value
redis 127.0.0.1:6379> del key1 (integer) 1 redis 127.0.0.1:6379> get key1 (nil) redis 127.0.0.1:6379> set key1 "20" OK redis 127.0.0.1:6379> decrby key1 (error) ERR wrong number of arguments for 'decrby' command redis 127.0.0.1:6379> get key1 "20" redis 127.0.0.1:6379> decrby key1 5 (integer) 15 redis 127.0.0.1:6379> decrby key1 4 (integer) 11 redis 127.0.0.1:6379> get key1 "11" redis 127.0.0.1:6379>
六、get key
redis> GET nonexisting (nil) redis> SET mykey "Hello" OK redis> GET mykey "Hello" redis>
七、getbit key offset
redis> SETBIT mykey 7 1 (integer) 0 redis> GETBIT mykey 0 (integer) 0 redis> GETBIT mykey 7 (integer) 1 redis> GETBIT mykey 100 (integer) 0 redis>
八、getrange key start and
redis> SET mykey "This is a string" OK redis> GETRANGE mykey 0 3 "This" redis> GETRANGE mykey -3 -1 "ing" redis> GETRANGE mykey 0 -1 "This is a string" redis> GETRANGE mykey 10 100 "string" redis>
九、getset key value
(integer) 0 redis 127.0.0.1:6379> del key1 (integer) 1 redis 127.0.0.1:6379> incr key1 (integer) 1 redis 127.0.0.1:6379> get key1 "1" redis 127.0.0.1:6379> getset key1 "0" "1" redis 127.0.0.1:6379> get key1 "0" redis 127.0.0.1:6379> set key2 "hello" OK redis 127.0.0.1:6379> getset key2 "sdgou.com" "hello" redis 127.0.0.1:6379> get key2 "sdgou.com" redis 127.0.0.1:6379>
十、incr key
redis 127.0.0.1:6379> set mykey "10" OK redis 127.0.0.1:6379> get mykey "10" redis 127.0.0.1:6379> incr mykey (integer) 11 redis 127.0.0.1:6379> get mykey "11" redis 127.0.0.1:6379>
十一、incrby key increment
edis> SET mykey "10" OK redis> INCRBY mykey 5 (integer) 15 redis>
十二、incrbyfloat key increment
redis> SET mykey 10.50 OK redis> INCRBYFLOAT mykey 0.1 "10.6" redis> SET mykey 5.0e3 OK redis> INCRBYFLOAT mykey 2.0e2 "5200" redis>
1三、mget key [key...]
redis> SET key1 "Hello" OK redis> SET key2 "World" OK redis> MGET key1 key2 nonexisting 1) "Hello" 2) "World" 3) (nil) redis>
1四、mset key value [key value ...]
edis> MSET key1 "Hello" key2 "World" OK redis> GET key1 "Hello" redis> GET key2 "World" redis>
1五、msetnx key value [key value ...]
redis> MSETNX key1 "Hello" key2 "there" (integer) 1 redis> MSETNX key2 "there" key3 "world" (integer) 0 redis> MGET key1 key2 key3 1) "Hello" 2) "there" 3) (nil) redis>
1六、psetex key milliseconds value
redis> PSETEX mykey 1000 "Hello" OK redis> PTTL mykey (integer) 998 redis> GET mykey "Hello" redis>
1七、set key value [EX seconds] [PX milliseconds] [NX|XX]
edis> SET mykey "Hello" OK redis> GET mykey "Hello" redis>
18 、setbit key offset value
redis> SETBIT mykey 7 1 (integer) 0 redis> SETBIT mykey 7 0 (integer) 1 redis> GET mykey "\u0000" redis>
1九、setex key seconds value
redis> SETEX mykey 10 "Hello" OK redis> TTL mykey (integer) 10 redis> GET mykey "Hello" redis>
20 、setnx key value
edis> SETNX mykey "Hello" (integer) 1 redis> SETNX mykey "World" (integer) 0 redis> GET mykey "Hello" redis>
21 setrange key offset value
redis> SET key1 "Hello World" OK redis> SETRANGE key1 6 "Redis" (integer) 11 redis> GET key1 "Hello Redis" redis>
2二、strlen key
redis> SET mykey "Hello world" OK redis> STRLEN mykey (integer) 11 redis> STRLEN nonexisting (integer) 0 redis>