Common redis operations

Memcache

Memcache is not persistent
It supports simple key value mode and single type
It is generally used as a cache database and a secondary persistent database

Redist

It covers almost most functions of Memcache.
It supports data persistence and is mainly used for data backup and recovery
In addition to supporting the simple key value mode, it also supports the storage of a variety of data structures, such as list set, hash, Zset, etc
Generally, it is used as a cache database and an auxiliary persistent session database

Common commands

tar zxvf 'file name'

Redis server foreground open

Enable redis in the background

1,go redis.confg File search daemonize yes  If you search, you can use/ then: wq! preservation
2,get into/usr/loacl/bin 
3,redis-server /etc/redis.conf  Start command
ps -ef | grep redis  View process
redis-cli You can connect to our through the client Redis
redis-cli shutdown Can be closed redis

memchache Vs multithreading + lock vs redis single thread + multiplex IO
memchache supports a single type, and redis supports multiple types
memchache supports multi thread + lock mechanism, and redis supports single line + multiplex IO
memchache does not support persistence. redis supports persistence

key value common commands

keys * view all key s
Set key val
exists key determines whether the key exists. 1 means yes, 0 means No
type key what type is the judgment key
del delete key
unlink key will be deleted in asynchronous operation
expire key sets the expiration time of the key
ttl key to see how many seconds the key has expired, - 1 means it does not expire, and - 2 means it has expired
stnx cannot be overwritten. It can only be set when the value does not exist
incr key increases your value by one
decr key reduces your value by one

incrby key 10 can be set by adding 10 each time
decrby key 10 can be set by subtracting 10 each time
mset k1 v1 k2 v2 can set multiple settings at the same time
Mget K1, K2 and K3 can obtain multiple data at the same time
msetnx sets multiple at the same time. It is atomic. If one fails, it will fail

Five basic data types commonly used in redis

###String
The string type is binary safe, which means that Redis's string can contain any number of plays, such as JPG pictures or serialized objects.
The string value in a Redis can be 512M at most.

List

lpush left
rpush right
lrange 0 -1 means take all

lpop takes the value from the left
rpop takes the value from the right (the key does not exist after taking it out). The value is in the key, and the value is dead

Rpop lpush takes values from the left and places them on the right

lindex key gets the specified subscript
linsert key before v1 v11, add a value before v1
lrem (the same can be deleted)
lset (can be replaced)

set

set Provided functions and list Similar to a list function, except that set The weight can be discharged automatically.
When you need to store a list of data and don't want duplicate data, set Is a good choice.
set yes string An unordered collection of types whose underlying layer is a hash Table, so the complexity of adding and deleting lookup is O(1)You can find it by looking once

Common commands
sadd key v1 v2
smembers key Fetch the set of values
sismember k1 v1 judge k1 Does it exist v1 This value
scard Returns the number of modified elements
srem Delete an element in the collection
spop key Casually from key It spits out a value
srandmember Randomly extract from the set N Values. It is not removed from the collection.
smove Move one value to another

hash type

hash Type is especially suitable for storing objects
hset key user(Fields) value(value)
hget key user(field)
hmset (Multiple values can be stored)
hexists(Check if a value exists)
hincby Plus 1

Zset ordered set type

zset Is an ordered collection, a collection of strings without duplicate elements
zadd(Add values to it)
zrange key Value 0-1 withscores(Through this, his score and value can be removed)  The default is to sort from small to large automatically

New data type Bitmaps

redis transaction

redis Transaction is a separate isolation operation: all commands will be serialized and executed in order. During the execution of the transaction, it will not be interrupted by command requests sent by other clients.
multi Start transaction
exex implement
discard  Cancel execution
watch Monitor one key

redis persistent session operation

RDB It's a kind of redis In the persistent session mode, the snapshot of the data set in memory is written to the disk within the specified time interval, and the snapshot file can be directly read into memory when it is restored
RDB Disadvantages: data may be lost at the last time
 to configure redis.conf Can be set every 30 S How much is generated K After the configuration is completed, please restart.
save It is not recommended to start persistence manually. If you use virtualization, it will only perform persistence operations, and others will be blocked

AOF
 Each write operation (incremental save) is recorded in the form of a log redis Execute all write instructions and record them (read operations are not recorded). You only need to ask for price, but you can't rewrite the file
AOF Direct access is not enabled by default redis.conf modify appendonly.aof
 If both are enabled at the same time, the system will default to AOF Data

redis master-slave replication

After the host data is updated, it is automatically synchronized to the standby computer according to the configuration policy master/slaver Mechanism, Master Mainly writing. Slave Mainly reading.(It is equivalent to read-write separation)
characteristic
1,Read write separation( Master Mainly writing. Slave (mainly reading)
2,Fast disaster recovery (when one slave hangs up, I can quickly switch to other slave libraries)
3,Generally, there are one master and many slaves. One hangs up and can be switched at will.

welfare

Yishoumuke.com, geek time, class bar, hook, Everest, horse soldier, dark horse, Netease cloud, Tencent classroom, Dachang college, shangsilicon Valley java,golang,python,Front end, big data courses! All of them! HD no secret! fifty T of IT The development of curriculum resources is free today. I have a share
 Weixin:PP666888LS

Tags: Database Redis Cache

Posted on Thu, 28 Oct 2021 22:09:45 -0400 by slweb