mac os x用macport安装redis
一、Redis简要介绍
Redis是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。从2010年3月15日起,Redis的开发工作由VMware主持。
2性能
如果真要把Redis与Memcached进行对比,参考下图:
使用Memcached,让我感触颇深的是Object Size的问题,由于SQL未作优化直接映射对象,导致缓存对象大于1MB,Memcached就抛了异常。而Redis默认缓存对象512MB,最大支持1GB。至少在缓存对象时,可以有更大的伸缩空间了! 此外,是数据类型。Memcached比较简单,而Redis可以支持更多复杂的数据类型,如HASH、SET、SortedSet等等。
二、安装使用
1、安装操作命令
port install redis
2、运行redis命令
redis-server /opt/local/etc/redis.conf
3、查看是否按照成功、使用使用缓存操作。
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>
三、Redis常用操作使用命令。
1、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>
2、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>
3、bitop operation destkey key [key...]
- bitop and destkey srckey1 srckey2 srckey3 ... srckeyN
- bitop or destkey srckey1 srckey2 srckey3 ... srckeyN
- bitop xor destkey srckey1 srckey2 srckey3 ... srckeyN
- bitop not destkey srckey
位运算,按位与、按位或、按位异或、按位非
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"
4、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>
5、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>
6、get key
redis> GET nonexisting (nil) redis> SET mykey "Hello" OK redis> GET mykey "Hello" redis>
7、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>
8、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>
9、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>
10、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>
11、incrby key increment
edis> SET mykey "10" OK redis> INCRBY mykey 5 (integer) 15 redis>
12、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>
13、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>
14、mset key value [key value ...]
edis> MSET key1 "Hello" key2 "World" OK redis> GET key1 "Hello" redis> GET key2 "World" redis>
15、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>
16、psetex key milliseconds value
redis> PSETEX mykey 1000 "Hello" OK redis> PTTL mykey (integer) 998 redis> GET mykey "Hello" redis>
17、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>
19、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>
22、strlen key
redis> SET mykey "Hello world" OK redis> STRLEN mykey (integer) 11 redis> STRLEN nonexisting (integer) 0 redis>