【工具库】--redis 安装+使用示例+安全(181)

来源:互联网 发布:linux c 编译器 编辑:程序博客网 时间:2024/06/03 17:52

1 安装
//ubuntu 操作系统

$sudo apt-get update $sudo apt-get install redis-server$redis-server$redis-cli 

redis 127.0.0.1:6379> ping
PONG

redis 127.0.0.1:6379> CONFIG GET *

redis 127.0.0.1:6379> CONFIG SET loglevel “notice”
OK
redis 127.0.0.1:6379> CONFIG GET loglevel
1) “loglevel”
2) “notice”

2 操作数据类型 5种
Redis supports 5 types of data types. strings, hashes, lists, sets and sorted sets

1 strings
redis 127.0.0.1:6379> SET name “tutorialspoint”
OK
redis 127.0.0.1:6379> GET name
“tutorialspoint”
Note − A string value can be at max 512 megabytes in length.

2 hashes:
A Redis hash is a collection of key value pairs. Redis Hashes are maps between string fields and string values. Hence, they are used to represent objects.

redis 127.0.0.1:6379> HMSET user:1 username tutorialspoint password
tutorialspoint points 200
OK
redis 127.0.0.1:6379> HGETALL user:1
1) “username”
2) “tutorialspoint”
3) “password”
4) “tutorialspoint”
5) “points”
6) “200”
Every hash can store up to 2的(32) - 1 field-value pairs (more than 4 billion).

3 lists
Redis Lists are simply lists of strings, sorted by insertion order. You can add elements to a Redis List on the head or on the tail.
redis 127.0.0.1:6379> lpush tutoriallist redis
(integer) 1
redis 127.0.0.1:6379> lpush tutoriallist mongodb
(integer) 2
redis 127.0.0.1:6379> lpush tutoriallist rabitmq
(integer) 3
redis 127.0.0.1:6379> lrange tutoriallist 0 10

1) “rabitmq”
2) “mongodb”
3) “redis”
The max length of a list is 2的(32) - 1 elements (4294967295, more than 4 billion of elements per list).

4 sets
Redis Sets are an unordered collection of strings. In Redis, you can add, remove, and test for the existence of members in O(1) time complexity.
redis 127.0.0.1:6379> sadd tutoriallist redis
(integer) 1
redis 127.0.0.1:6379> sadd tutoriallist mongodb
(integer) 1
redis 127.0.0.1:6379> sadd tutoriallist rabitmq
(integer) 1
redis 127.0.0.1:6379> sadd tutoriallist rabitmq
(integer) 0
redis 127.0.0.1:6379> smembers tutoriallist

1) “rabitmq”
2) “mongodb”
3) “redis”

Note − In the above example, rabitmq is added twice, however due to unique property of the set, it is added only once.

The max number of members in a set is 2的(32) - 1 (4294967295, more than 4 billion of members per set).

5 sorted sets

redis 127.0.0.1:6379> zadd tutoriallist 0 redis
(integer) 1
redis 127.0.0.1:6379> zadd tutoriallist 0 mongodb
(integer) 1
redis 127.0.0.1:6379> zadd tutoriallist 0 rabitmq
(integer) 1
redis 127.0.0.1:6379> zadd tutoriallist 0 rabitmq
(integer) 0
redis 127.0.0.1:6379> ZRANGEBYSCORE tutoriallist 0 1000

1) “redis”
2) “mongodb”
3) “rabitmq”

3 安全
127.0.0.1:6379> config get requirepass
1) “requirepass”
2) “”
127.0.0.1:6379> config set requirepass “zw”
OK
127.0.0.1:6379> config get requirepass
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth zw
OK
127.0.0.1:6379> config get requirepass
1) “requirepass”
2) “zw”
127.0.0.1:6379>

Run Commands on the Remote Server
$ redis-cli -h host -p port -a password

Following example shows how to connect to Redis remote server, running on host 127.0.0.1, port 6379 and has password mypass.

$redis-cli -h 127.0.0.1 -p 6379 -a “mypass”
redis 127.0.0.1:6379>
redis 127.0.0.1:6379> PING
PONG

0 0