1.Redis in-memory database

  • Redis is based on a memory-persistent logging, key-value database
  • Data types stored (string, int, float, list,set, hash)
  • Redis is widely used in caches and queues
  • Atomicity -Redis all operations are atomic and support transactions, not rollback
  • Data persistence – The ability to save in-memory data to disk and provide multiple storage mechanisms
  • Data backup – Supports master-slave mode
  • Clustering – Supports clustering and implements sentinel mechanism
  • Rich features – set expiration events, publish/subscribe, notification mechanisms and more

2. Application scenarios

  • Session storage (better performance, easy to set expiration time)
  • Shopping cart (record the user’s current shopping cart information)
  • Web page cache (store the web page information returned by the requested URL route in redis key-value)
  • Real-time ranking (Redis stores data weight information and provides ranking data)

3. The installation

Sudo apt-get install redis-serverCopy the code

4. Configuration

  • (No operation is required for the default configuration.)
Cat /etc/redis/redis.confCopy the code
# IP address bind 127.0.0.1 # port 6379 # Whether to run daemonize with daemonize yes # Logfile where log files (error messages) are stored "/var/log/redis/redis-server.log" # Data persistence filename dbfilename dump. RDB # Data persistence file storage path dir./ # Automatic persistence policy If 100 changes are made within 60 seconds, the system automatically saves the data once Save 60 100 save 900 1 # Whether to enable the aOF persistent mechanism appendonly no # database, default 16 database 16 # secondary Settings, specify the IP of the primary server + port slaveofCopy the code
If you want to modify log files, persistent file names, or paths, you must enable the permission for saving the file/path. Otherwise, the file/path fails to be saved. For example, run sudo chmod 777 /var/log/redis/redis-server.logCopy the code

5. Start

redis-serve
Copy the code

6. Server and client commands

  • Start the database
Redis-server # start redis-server with default configuration./redis.conf # Start redis-server with custom configurationCopy the code
  • Connecting to a Database
Redis -cli # connect to redis-cli -h 127.0.0.1 -p 6379 # Connect to the database of the specified service redis-cli --help # View the help documentationCopy the code
  • Connect the test
ping
Copy the code
  • Switching databases
select 15
Copy the code
  • Data persistence
Bgsave # Async persistent databaseCopy the code
  • Clearing the database
Flushdb flushes the current databaseCopy the code
  • Closing the database
Shutdown [NOSAVE] [SAVE] # shutdown [NOSAVECopy the code
  • Kill Redis database process
The lsof - I: 6379 # to check the database process pid or ps aux | grep redis kill 9 pid # end redis processCopy the code

7. Redis – CLI data operation

Redis method called | different data types

  • string

  • keys

  • hash

  • list

  • The set element is of type string, unordered, unique, and not modified

  • The zset element is a string, ordered set, associated with a socre of type double, representing all elements. The elements are sorted from smallest to largest without modification.

8. Python interaction

  • Creating a connection object
import redis
con = redis.Redis(host='localhost', port=6379, decode_responses=True, db=0)
Copy the code

demo1.py

import redis
try:
    con = redis.Redis(host='localhost', port=6379, decode_responses=True, db=0)
    Add key name to test
    a= con.set('name'.'test')Set method/increment
    b = con.get('name')#get method/check
    c = con.keys()# keys methods/keys
    d = con.delete('name')# delete method/delete
    print('write', a)  
    print('query', b)  
    print('keys', c)
    print('delete', d)  
except Exception as e:
    print(e)

Copy the code

demo2.py

import redis
try:
    con = redis.Redis(host='localhost', port=6379, decode_responses=True, db=0)
    Write data of type hashMap
    user1 = {"name":"zhangsan"."age":18}
    con.hmset('zhangsan', user1)
    content = con.hmget("zhangsan"."name") [0]
except Exception as e:
    print(e)

Copy the code