Load the image
The official version of the query image and information
$ docker search redis
Loading recent image
$ docker pull redis:lastest
Check local mirror
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/redis latest f0453552d7f2 7 days ago 98.2 MB
docker.io/mysql latest 9b51d9275906 2 weeks ago 547 MB
docker.io/tomcat latest 4e7840b49fad 3 weeks ago 529 MB
Run container
Enable daemon
$ docker run --name kris-redis -p 6380:6379 -d redis --requirepass "123456"
Custom redis.conf start
docker run -p 6379:6379 --name kris-redis -v /root/docker/redis/redis.conf:/etc/redis/redis.conf -v /root/docker/redis/data:/data -d redis redis-server /etc/redis/redis.conf --appendonly yes
# -p 6379:6379:把容器内的6379端口映射到宿主机6379端口
# -v /root/redis/redis.conf:/etc/redis/redis.conf:把宿主机配置好的redis.conf放到容器内的这个位置中
# -v /root/redis/data:/data:把redis持久化的数据在宿主机内显示,做数据备份
# redis-server /etc/redis/redis.conf:这个是关键配置,让redis不是无配置启动,而是按照这个redis.conf的配置启动
# -appendonly yes:redis启动后数据持久化
View the running status
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3c90175b38e6 redis "docker-entrypoint..." 13 minutes ago Up 13 minutes 0.0.0.0:6380->6379/tcp kris-redis
2bbd52391bab mysql "docker-entrypoint..." 7 days ago Up 7 days 0.0.0.0:3306->3306/tcp, 33060/tcp kris-mysql
Enable redis-cli, namely redis client
$ docker exec -it kris-redis redis-cli
Five Redis data types
Foreword
Redis keywords are not case sensitive, but not mixed case.
Redis is stored in the form of key-value pairs.
Five Redis data types: strings, lists, sets, hash, ordered collection
Redis key
Common:
Case:
keys *
: Return all "top-level key", i.e., the name of the major data type of data stored (Note: the Hash is a key-value pair, but also a key-value pair Value, <KEY, <K, V >>), chestnut give :
127.0.0.1:6379>$ keys *
1) "customer" # 定义的Hash
2) "set01" # 定义的Set
3) "list01" # 定义的List
4) "username" # 定义的String
exists
: To determine whether there is a key
127.0.0.1:6379>$ exists username
(integer) 1 # 存在返回1
127.0.0.1:6379>$ exists username00
(integer) 0 # 不存在返回0
move key dbindex
: The current library (index: 1) there is no, is removed, the data is migrated to other databases
NOTE: redis.conf default database 16, the default is 0, 0 is the initial library
127.0.0.1:6379>
, when selecting the other libraries, the library is such that127.0.0.1:6379[dbindex]>
the index represented dbindex library.
127.0.0.1:6379>$ move username 2 # 数据迁移至其他库
(integer) 1 # 迁移成功
127.0.0.1:6379>$ select 2 # 选择当前操作的数据库
OK
127.0.0.1:6379[2]>$ get username # 查询迁移后的数据,若查询1库中数据,需要重新选择库
"kris"
expire key 秒钟
: Set the expiration time for the given key
ttl key
: Check how many seconds expired, -1 means never expires, -2 is expired
127.0.0.1:6379[2]>$ expire username 10 # 存活10秒
127.0.0.1:6379[2]>$ ttl username
(integer) 7
127.0.0.1:6379[2]>$ ttl username
(integer) 2
127.0.0.1:6379[2]>$ ttl username
(integer) -2 # 已过期
127.0.0.1:6379[2]>$ get username
(nil) # 已经不存在了
type key
: Check your key is what type
127.0.0.1:6379>$ type list01
list
String
Overview:
- Redis string type is the most basic data structure, the first key is a string type , and several other structures are built on the basis of the character string type, string type is the base type of the data structure of the other four .
- String type can actually string (simple string, complex strings (xml, json), numbers (integer, floating point), binary (images, audio, video)), but the maximum can not exceed 512M .
scenes to be used:
- Cache Functions: The most classic string usage scenario, redis layer as a cache , Mysql as a storage layer , most of the request data is acquired redis, since redis a support high concurrency characteristics, the cache can typically play acceleration and read reducing the pressure back-end role. Why redis have characteristics support high concurrency? .
- Counter: Many application will use redis counted as basic tools, he can achieve quick count, query cache function, while data can be further ground to other data sources. Such as: the number of video playback system is used as the base component redis count number of the video player.
- Sharing session: consideration for load balancing, distributed services access user information will be balanced to a different server, users may need to refresh the access log back in, you can avoid this problem by redis user session centralized management, in which under modes as long as the guarantee of high availability and scalability redis each time get users to update or query the login information are acquired directly from the centralized redis in.
- Speed: For safety reasons, each time you log allows users to enter the phone code, for SMS interface is not frequently accessed, the user can limit the frequency of verification codes per minute.
Commonly used commands:
Case:
set/get/del/append/strlen
When you define, if it already exists, it will save coverage
127.0.0.1:6379>$ set username kris
OK
127.0.0.1:6379>$ get username
"kris"
127.0.0.1:6379>$ append username louis
(integer) 9
127.0.0.1:6379>$ get username
"krislouis"
127.0.0.1:6379>$ strlen username
(integer) 9
List
Overview:
- List type is used to store ordered plurality of strings, each string in the list becomes element (Element), a list can store up to $ 2 ^ {32} $ -1 elements, in redis can queue table ends were inserted (pubsh) and pop (pOP), can also obtain the specified range of elements in the list, obtaining an element specified index table and the like, the list is a more flexible data structure that can act as stacks and queues role, in there are a lot of actual development scenarios.
- Advantages:
1. The elements of the list is ordered, which means you can get a list of elements or within a certain range by index index.
2. The list of elements within can be repeated.
scenes to be used:
Message Queuing: redis lpush + brpop the command queue blocking composition can be realized, the producer client is inserted from the left side list element with lupsh, "grab" when the end of the list of elements of the plurality of consumer clients using brpop command is blocked, multiple clients to ensure that the consumer's load balancing and high availability.
- Use the list of tips:
lpush+lpop=Stack(栈)
lpush+rpop=Queue(队列)
lpush+ltrim=Capped Collection(有限集合)
lpush+brpop=Message Queue(消息队列)
Common: to be updated
Case: to be updated
Hash
Overview:
In redis hash type is the key itself is a key structure , such as:
value={{field1,value1},......{fieldN,valueN}}
scenes to be used:
- Characteristics: hash string structure relative to the sequence of the cache information more intuitive , and update operations more convenient on.
- Commonly used in the user information , such as management.
- Inadequate: Hash types and different relational databases, hash type is sparse , and relational database is fully structured, easy to implement complex relational queries, redis non-relational databases, relational databases need to simulate complex inquiries, development difficulties, high maintenance costs.
Common: to be updated
Case: to be updated
set
Overview:
- Collection type element is used to store a plurality of character strings, and lists, but the difference is not allowed duplicate set of elements, and elements of the collection are unordered, subscript index can not get the elements, in addition to supporting the collection Redis additions and deletions to change search within, and also supports multiple collections on the intersection, union, difference, and rational use of good collection type, can solve many practical problems in the actual development.
scenes to be used:
Label (tag): a collection of the type typical usage scenarios, such as a user more interested in entertainment, sports, and another may be interested in the news, these interests is the label, with these data we can get people the same label, and common interest user labels, these data are important for user experience and has strong user viscosity. (Relationship between the user and the label should be placed in a maintenance thing to perform, to prevent part of the command failures caused by inconsistent data)
sadd = tagging (tag)
SPOP / srandmember = Random Item (generate a random number, such as lottery)
Sadd = + Sinter social Graph (social needs)
Common: to be updated
Case: to be updated
Ordered set
Overview:
- Ordered collection and collections intrinsically linked, he kept a collection of members can not have duplicate features, but the difference was that the ordered set of elements can be ordered, but it and the list using the index subscript different sort it is that it gives each element of a set score, sort of basis. (Ordered set of elements can not be repeated, but csore can repeat it and his classmates learned a number can not be repeated, but test scores can be the same).
Lists, sets, ordered set of three similarities and differences
scenes to be used:
List: an ordered collection of classic usage scenarios.
Such as video sites to do rankings for user-uploaded videos, list maintenance may be many ways: by time, in accordance with the amount of players, according to the number obtained praise and so on.
Common: to be updated
Case: to be updated