Python 3 operation redis

Keywords: Database Redis EPEL yum

redis is also known as caching

1.redis is a key-value storage system with no fields for ForeignKey and ManyToMany.
2. Data created in redis are not related to each other, so they are also called non-relational databases.
3. It supports the storage of data types including string, list, set, zset(sorted set -- ordered set) and hash (hash type).
4. The data types supported by redis all support push/pop, add/remove, take-intersection, merge and difference sets, and more abundant operations, and these operations are atomic.
5.redis supports sorting in various ways. To ensure efficiency, data is cached in memory.
6.redis periodically writes the updated data to disk or writes the modification operation to the additional record file, and realizes master-slave synchronization on this basis.

In a normal server, redis runs in memory, while the database runs on the server's hard disk.

Because memory runs much faster than hard disk speed, redis queries are much faster than databases stored on hard disk.

Similarly, because redis is stored in memory, a server can configure a very large hard disk. Compared with a hard disk, the configuration capacity of the server memory is much smaller than the configuration capacity of the hard disk.

So redis has some capacity limitations

Also because redis are stored in memory, the data stored in redis can easily be lost once the server is powered off.

redis stores data that is commonly used and not frequently updated

When a server comes online, when users access the data on the server, they first go to the cache to see if there is any data to query.

If the data needed by the user is stored in the cache, the data needed is obtained directly from the cache.
If there is no data needed by the user in the cache, the data will be queried from the database and returned to the user. At the same time, the data will be saved in memory.
In this way, when the next user accesses the same data again, he will read the required data directly from the cache instead of accessing the database, thus speeding up the user's access speed.

For example, when we publish a blog in the blog garden, the blog just published will not appear on the blog garden's home page immediately. It will take several minutes before the blog article just published appears on the blog garden's home page, which is the reason for caching.

1. Preparations

Configure epel source for CentOS system

You can refer to my blog CentOS7 System Configuration of Domestic yum Source and epel Source

2. Installing Redis

[root@bogon yum.repos.d]# yum list | grep redis
Repository base is listed more than once in the configuration
Repository updates is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
redis.x86_64                            3.2.10-2.el7                   @epel    
collectd-redis.x86_64                   5.7.1-2.el7                    epel     
collectd-write_redis.x86_64             5.7.1-2.el7                    epel     
hiredis.x86_64                          0.12.1-1.el7                   epel     
hiredis-devel.x86_64                    0.12.1-1.el7                   epel     
opensips-redis.x86_64                   1.10.5-3.el7                   epel     
pcp-pmda-redis.x86_64                   3.11.8-7.el7                   base     
php-nrk-Predis.noarch                   1.0.4-1.el7                    epel     
php-pecl-redis.x86_64                   2.2.8-1.el7                    epel     
php-phpiredis.x86_64                    1.0.0-2.el7                    epel     
python-redis.noarch                     2.10.3-1.el7                   epel     
python-trollius-redis.noarch            0.1.4-2.el7                    epel     
python2-django-redis.noarch             4.3.0-1.el7                    epel     
redis-trib.noarch                       3.2.10-2.el7                   epel     
rubygem-redis.noarch                    3.2.1-2.el7                    epel     
rubygem-redis-doc.noarch                3.2.1-2.el7                    epel     
syslog-ng-redis.x86_64                  3.5.6-3.el7                    epel     
uwsgi-logger-redis.x86_64               2.0.15-1.el7                   epel     
uwsgi-router-redis.x86_64               2.0.15-1.el7                   epel     
[root@bogon yum.repos.d]# yum install -y redis
Loaded plugins: fastestmirror, langpacks
Repository base is listed more than once in the configuration
Repository updates is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
Loading mirror speeds from cached hostfile
 * epel: ftp.cuhk.edu.hk
Package redis-3.2.10-2.el7.x86_64 already installed and latest version
Nothing to do

3. The file directory structure of redis

Use the rpm-ql command to view the files generated in the system after redis installation

[root@bogon ~]# rpm -ql redis
/etc/logrotate.d/redis
/etc/redis-sentinel.conf            # The daemon configuration file of redis
/etc/redis.conf                 # redis configuration file
/etc/systemd/system/redis-sentinel.service.d
/etc/systemd/system/redis-sentinel.service.d/limit.conf
/etc/systemd/system/redis.service.d
/etc/systemd/system/redis.service.d/limit.conf
/usr/bin/redis-benchmark
/usr/bin/redis-check-aof
/usr/bin/redis-check-rdb
/usr/bin/redis-cli                  # redis interactive command line interface
/usr/bin/redis-sentinel         # The Guardian file of redis
/usr/bin/redis-server           # redis server-side startup file
/usr/lib/systemd/system/redis-sentinel.service  # Function library file of redis daemon
/usr/lib/systemd/system/redis.service           # redis function library file
/usr/libexec/redis-shutdown     # Stop redis execution file
/usr/share/doc/redis-3.2.10     # redis Help File
/usr/share/doc/redis-3.2.10/00-RELEASENOTES
/usr/share/doc/redis-3.2.10/BUGS
/usr/share/doc/redis-3.2.10/CONTRIBUTING
/usr/share/doc/redis-3.2.10/MANIFESTO
/usr/share/doc/redis-3.2.10/README.md
/usr/share/licenses/redis-3.2.10            # licenses description of redis
/usr/share/licenses/redis-3.2.10/COPYING
/usr/share/man/man1/redis-benchmark.1.gz    # redis man file
/usr/share/man/man1/redis-check-aof.1.gz
/usr/share/man/man1/redis-check-rdb.1.gz
/usr/share/man/man1/redis-cli.1.gz
/usr/share/man/man1/redis-sentinel.1.gz
/usr/share/man/man1/redis-server.1.gz
/usr/share/man/man5/redis-sentinel.conf.5.gz
/usr/share/man/man5/redis.conf.5.gz
/var/lib/redis                          # Persistent file save path of redis
/var/log/redis                          # redis log file
/var/run/redis                          # redis process PID file

4. Start Stop and Status View of redis

[root@bogon ~]# systemctl status redis.service      # Looking at the redis status, you can see that it is active
● redis.service - Redis persistent key-value database
 Loaded: loaded (/usr/lib/systemd/system/redis.service; disabled; vendor preset: disabled)
Drop-In: /etc/systemd/system/redis.service.d
     └─limit.conf
 Active: active (running) since Thu 2017-11-16 17:29:39 CST; 1min 44s ago
Process: 6272 ExecStop=/usr/libexec/redis-shutdown (code=exited, status=0/SUCCESS)
Main PID: 6286 (redis-server)
 CGroup: /system.slice/redis.service
     └─6286 /usr/bin/redis-server 0.0.0.0:6379

Nov 16 17:29:39 bogon systemd[1]: Started Redis persistent key-value database.
Nov 16 17:29:39 bogon systemd[1]: Starting Redis persistent key-value database...
[root@bogon ~]# systemctl stop redis.service      # Stop redis
[root@bogon ~]# systemctl status redis.service      # The inactive state of redis
● redis.service - Redis persistent key-value database
 Loaded: loaded (/usr/lib/systemd/system/redis.service; disabled; vendor preset: disabled)
Drop-In: /etc/systemd/system/redis.service.d
     └─limit.conf
 Active: inactive (dead)

Nov 16 17:21:56 bogon systemd[1]: redis.service: control process exited, code=exited status=1
Nov 16 17:21:56 bogon systemd[1]: Unit redis.service entered failed state.
Nov 16 17:21:56 bogon systemd[1]: redis.service failed.
Nov 16 17:22:04 bogon systemd[1]: Started Redis persistent key-value database.
Nov 16 17:22:04 bogon systemd[1]: Starting Redis persistent key-value database...
Nov 16 17:29:39 bogon systemd[1]: Stopping Redis persistent key-value database...
Nov 16 17:29:39 bogon systemd[1]: Started Redis persistent key-value database.
Nov 16 17:29:39 bogon systemd[1]: Starting Redis persistent key-value database...
Nov 16 17:31:38 bogon systemd[1]: Stopping Redis persistent key-value database...
Nov 16 17:31:38 bogon systemd[1]: Stopped Redis persistent key-value database.
[root@bogon yum.repos.d]# redis-server        # Start redis program
5587:C 16 Nov 15:42:56.010 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
5587:M 16 Nov 15:42:56.018 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.2.10 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 6519
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                        

5587:M 16 Nov 15:42:56.022 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
5587:M 16 Nov 15:42:56.022 # Server started, Redis version 3.2.10
5587:M 16 Nov 15:42:56.022 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
5587:M 16 Nov 15:42:56.025 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
5587:M 16 Nov 15:42:56.026 * The server is now ready to accept connections on port 6379
[root@bogon ~]# systemctl status redis.service
● redis.service - Redis persistent key-value database
 Loaded: loaded (/usr/lib/systemd/system/redis.service; disabled; vendor preset: disabled)
Drop-In: /etc/systemd/system/redis.service.d
     └─limit.conf
 Active: active (running) since Thu 2017-11-16 17:31:47 CST; 2s ago
Main PID: 6325 (redis-server)
 CGroup: /system.slice/redis.service
     └─6325 /usr/bin/redis-server 0.0.0.0:6379

Nov 16 17:31:47 bogon systemd[1]: Started Redis persistent key-value database.
Nov 16 17:31:47 bogon systemd[1]: Starting Redis persistent key-value database...

There are two ways to start redis:

[root@bogon ~]# systemctl start redis.service   # Background boot, no welcome interface
[root@bogon ~]# redis-server            # Front-end boot, you can see the welcome interface

There are two ways to stop redis:

[root@bogon ~]# systemctl stop redis.service
[root@bogon ~]# /usr/libexec/redis-shutdown

5. Description of redis master configuration file

bind 127.0.0.1                          # redis-bound host address, which by default only supports local connections
protected-mode yes                      # Whether to run redis in protected mode
port 6379                               # Specify the port redis listens on
tcp-backlog 511
timeout 0                               # How long does the client remain idle and close the connection at 0:00?
tcp-keepalive 300                       # Maximum number of connections allowed in tcp mode
daemonize no                            # Is it running as a daemon
supervised no                           # Do not use monitoring trees
pidfile /var/run/redis_6379.pid         # The redis runtime saves pid files
loglevel verbose                        # Log file recording mode, default to standard output
logfile /var/log/redis/redis.log        # Save path of redis log file
databases 16                            # Number of redis databases in the system
save 900 1                              # When there is an update operation in 900 seconds in redis, the data is synchronized to the file and saved.
save 300 10                             # When redis has 10 update operations in 300 seconds, it synchronizes the data to a file to save.
save 60 10000                           # When redis has 10,000 updates in 60 seconds, it synchronizes the data to a file for storage.
stop-writes-on-bgsave-error yes
rdbcompression yes                      # Whether to start compression when data in redis is saved to local database by default yes
rdbchecksum yes
dbfilename dump.rdb                     # File name of local database
dir /var/lib/redis                      # Path of local database
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendonly no                           # Whether to log every update operation
appendfilename "appendonly.aof"         # Specify the file name for the update log
appendfsync everysec                    # Synchronize data in redis to local files once a second
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60    #
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

6.redis interactive interface

[root@bogon ~]# redis-cli   # Interactive interface into redis
127.0.0.1:6379> 

As you can see, we have entered the interactive environment of redis.

127.0.0.1:6379> set k1 v1   # Set the value of k1 to v1
OK
127.0.0.1:6379> get k1      # Get the value of k1
"v1"    
127.0.0.1:6379> set k2 v2   # Set the value of k2 to v2
OK
127.0.0.1:6379> get k2      # Get the value of k2
"v2"

7. Python 3 operation redis

Open redis configuration file / etc/redis.conf in linux system

Amend line 61 to read

bind 0.0.0.0

Represents that all hosts can be connected

Then restart redis to make the configuration file effective

[root@bogon ~]# systemctl restart redis.service

Install redis module in windows system

pip3 install redis

Create a new redis_test.py file with the contents of

import redis

r1=redis.Redis(host="192.168.16.220",port=6379)

print("k1 value read for the first time:", "r1.get("k1")))
print("k2 value read for the first time:", "r1.get("k2")))
r1.set("k3","v3")

r1.delete("k1")
r1.delete("k2")

print("Get the value of K1 for the second time", r1.get("k1")
print("Get the value of K2 for the second time", r1.get("k2")
print(r1.get("k3"))

Operation results:

k1 value read for the first time: b'v1'
k2 value read for the first time: b'v2'
Get the value of k1 for the second time None
 The second acquisition of the value of k2 None
b'v3'

Get the values of k1 and k2 again at the command prompt of linux

127.0.0.1:6379> get k1
(nil)
127.0.0.1:6379> get k2
(nil) 

Because the values of k1 and k2 have been deleted in redis_test.py, the value obtained is None.

8. Benefits of Redis

(1) Fast speed, because data exists in memory, similar to HashMap, HashMap has the advantage that the time complexity of search and operation is O(1)
(2) Support rich data types, string, list, set, sorted set, hash
 (3) Supporting transactions, operations are atomic, so-called atomicity is the change of data or all of them are executed, or none of them are executed.
(4) Rich features: can be used for caching, messages, set expiration time by key, after expiration will be automatically deleted

Posted by coops on Thu, 31 Jan 2019 14:09:15 -0800