graph LR
id[如果客户端想通过一个缓存系统取回value中的某一个元素] --> id1( memeorycache 将所有的value返回,需要server 网卡IO 客户端自己写解析方法)
id --> id2(类型不是很重要,redis中对每种类型都有自己的方法 index Ipop)
安装redis
下载
1 2 3
wget http://download.redis.io/releases/redis-6.0.6.tar.gz tar xzf redis-6.0.6.tar.gz cd redis-6.0.6
编译安装
阅读README
1
make
如果编译失败运行一下 make distclean
企业还需要 make test 测试一下编译流程
安装(实际就是将可执行程序迁入目标目录)
1
make install PREFIX = /opt/bigdata/module/redis5
配置环境变量,REDIS_HOME=/opt/bigdata/module/redis5
redis 服务化
安装包内服务:
进入到解压后的 src 目录,通过如下命令启动Redis:
1
src/redis-server
服务常驻化:
进入redis的
在utils文件夹里面有install_server执行
1 2 3 4 5
Please select the redis port for this instance: [6379] Please select the redis config file name [/etc/redis/6379.conf] Please select the redis log file name [/var/log/redis_6379.log] Please select the data directory for this instance [/var/lib/redis/6379] Please select the redis executable path []
1 2 3 4 5
Installing service... Successfully added to chkconfig! Successfully added to runlevels 345! Starting Redis server... Installation successful!
127.0.0.1:6379> set k380:1 hello OK 127.0.0.1:6379> get k380:1 "hello" 127.0.0.1:6379> select 8 OK 127.0.0.1:6379[8]> get k380:1 (nil) 127.0.0.1:6379[8]>
127.0.0.1:6379> set k1 hello OK 127.0.0.1:6379> get k1 "hello" 127.0.0.1:6379> help set
SET key value [expiration EX seconds|PX milliseconds] [NX|XX] summary: Set the string value of a key since: 1.0.0 group: string
127.0.0.1:6379> set k1 ooxx nx \\nx如果存在,不插入 分布式锁的时候使用 (nil) 127.0.0.1:6379> get k1 "hello" 127.0.0.1:6379> set k2 hello xx \\xx只能更新 (nil) 127.0.0.1:6379> get k2 (nil)
127.0.0.1:6379> mset k3 a k4 b OK 127.0.0.1:6379> get k3 "a" 127.0.0.1:6379> get k4 "b" 127.0.0.1:6379> mget k3 k4 1) "a" 2) "b"
127.0.0.1:6379> get k1 "hello" 127.0.0.1:6379> append k1 " world" (integer) 11 127.0.0.1:6379> get k1 "hello world"
127.0.0.1:6379> type k1 string 127.0.0.1:6379> set k2 hello OK 127.0.0.1:6379> type k2 string 127.0.0.1:6379> object help 1) OBJECT <subcommand> arg arg ... arg. Subcommands are: 2) ENCODING <key> -- Return the kind of internal representation used in order to store the value associated with a key. 3) FREQ <key> -- Return the access frequency index of the key. The returned integer is proportional to the logarithm of the recent access frequency of the key. 4) IDLETIME <key> -- Return the idle time of the key, that is the approximated number of seconds elapsed since the last access to the key. 5) REFCOUNT <key> -- Return the number of references of the value associated with the specified key. 127.0.0.1:6379> object encoding k2 "embstr" 127.0.0.1:6379> get k2 "hello" 127.0.0.1:6379> object encoding k1 "int" 127.0.0.1:6379> incr k1 (integer) 100 127.0.0.1:6379> incrby k1 22 (integer) 122 127.0.0.1:6379> get k1 "122" 127.0.0.1:6379> decr k1 (integer) 121 127.0.0.1:6379> decrby k1 22 (integer) 99 127.0.0.1:6379> incrbyfloat k1 0.5 "99.5" 127.0.0.1:6379> get k1 "99.5" 127.0.0.1:6379> set k3 jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj OK 127.0.0.1:6379> object encoding k3 "embstr" 127.0.0.1:6379> append k3 jjjjj (integer) 45 127.0.0.1:6379> object encoding k3 "raw"
127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> set k1 hello OK 127.0.0.1:6379> STRLEN k1 (integer) 5 127.0.0.1:6379> set k2 9 OK 127.0.0.1:6379> OBJECT encoding k2 "int" 127.0.0.1:6379> STRLEN k2 (integer) 1 127.0.0.1:6379> APPEND k2 999 (integer) 4 127.0.0.1:6379> get k2 "9999" 127.0.0.1:6379> object encoding k2 "raw" 127.0.0.1:6379> INCR k2 (integer) 10000 127.0.0.1:6379> OBJECT encoding k2 "int" 127.0.0.1:6379> STRLEN k2 (integer) 5 127.0.0.1:6379> set k3 a OK 127.0.0.1:6379> get k3 "a" 127.0.0.1:6379> STRLEN k3 (integer) 1 127.0.0.1:6379> APPEND k3 中 (integer) 4 127.0.0.1:6379> STRLEN k3 (integer) 4
redis只拿字节流,数据不会被破坏
如果是字符流,各个客户端对数据宽度的理解是不一样的,宽度不一致可能导致溢出问题
1 2 3 4
127.0.0.1:6379> set k1 99999 OK 127.0.0.1:6379> STRLEN k1 (integer) 5
即使是int值,redis也是编码成字节,如果对其调用INCR方法,它会先把字节转换成int值,
再进行INCR,如果incr成功了,然后再将其encoding成int
1 2 3 4
127.0.0.1:6379> set k2 中 OK 127.0.0.1:6379> STRLEN k2 (integer) 3
终端与redis的编码类型是UTF-8
如果改成GBK会变成2了
1 2
127.0.0.1:6379> get k2 "\xe4\xb8\xad"
1 2 3 4 5
[lemcoden@hadoop01 ~]$ redis-cli --raw 127.0.0.1:6379> get k3 涓 127.0.0.1:6379> get k2 中
–raw 编码集的格式化,默认utf-8
和Hbase一样是二进制安全的
!!一定要沟通好数据的编码和解码
成本问题
get和set是两个请求,getset减少一次IO通信
1 2 3 4 5 6 7 8
127.0.0.1:6379> set k1 hello OK 127.0.0.1:6379> get k1 hello 127.0.0.1:6379> GETSET k1 mashbing hello 127.0.0.1:6379> get k1 mashbing
原子性的操作
1 2 3 4 5 6 7 8 9 10 11
127.0.0.1:6379> MSETNX k1 a k2 b 1 127.0.0.1:6379> mget k1 k2 a b 127.0.0.1:6379> MSETNX k2 c k3 d 0 127.0.0.1:6379> mget k1 k2 k3 a b