Python3 operating Redis

Source: Internet
Author: User
Tags benchmark redis version server memory install redis redis server



Redis is also known as caching


1.redis is a key-value storage system with no fields for ForeignKey and ManyToMany.
2. The data created in redis has nothing to do with each other, so it is also called a non-relational database.
3. It supports storing data types including string (string), list (linked list), set (set), zset (sorted set - ordered set) and hash (hash type).
4. The data types supported by redis support push/pop, add/remove, and intersection set and difference sets and richer operations, and these operations are atomic.
5.redis supports sorting in a variety of different ways. To ensure efficiency, the data is cached in memory.
6. redis periodically writes the updated data to disk or writes the modification operation to the appended record file, and on this basis, master-slave synchronization is implemented.


In a normal server, Redis is running in memory, and the database is running on the server's hard disk



Because memory runs much faster than the speed of the hard disk, Redis queries are much faster than the database saved on the hard disk



Similarly, because Redis is stored in memory, a server can be configured with very large hard disks, and the configuration capacity of the server memory is much smaller than the configured hard disk capacity, compared to the hard disk.



So Redis has a certain capacity limit.



Also because Redis is stored in memory, the data stored in Redis is easily lost if the server loses power


Redis stores commonly used data that is not updated frequently


When a server is just on-line, when the user accesses the data on the server, first go to the cache to see if there is any data to query.


If the data stored by the user is saved in the cache, the required data is directly obtained from the cache;
If there is no data required by the user in the cache, the data will be queried from the database and returned to the user, and the data will be saved in memory;
In this way, when the next user accesses the same data again, it will directly read the required data from the cache without accessing the database, which speeds up the user's access speed.


For example, when we published a blog in the blog Park, the home page of the blog will not immediately appear just published the blog, but to wait a few minutes, in the blog park on the homepage will appear just published the blog post, this is because of the reason for caching.


1. Preparatory work


Configure the source for the CentOS system.epel



can refer to my blog CentOS7 system configuration 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.redis file directory structure


Use therpm -qlcommands to view the files that are generated on the system after Redis installation


[root@bogon ~]# rpm -ql redis
/etc/logrotate.d/redis
/etc/redis-sentinel.conf # redis daemon configuration file
/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 # redis daemon file
/usr/bin/redis-server # redis server startup file
/usr/lib/systemd/system/redis-sentinel.service # redis daemon's library file
/usr/lib/systemd/system/redis.service # redis function library file
/usr/libexec/redis-shutdown # Stop redis execution files
/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 # redis licenses description
/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 # redis
/var/log/redis # redis log file
/var/run/redis # redis process PID file
4.redis Start-Stop and Status View
[root@bogon ~]# systemctl status redis.service # View the status of redis, 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 # redis card inactive status
● 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 startup, no welcome interface
[root@bogon ~]# redis-server # The front desk starts, you can see the welcome screen


There are two ways to stop Redis:


[root@bogon ~]# systemctl stop redis.service
[root@bogon ~]# /usr/libexec/redis-shutdown
Description of the main configuration file for 5.redis
Bind 127.0.0.1 # redis bound host address, here only supports local connection
Protected-mode yes # Whether to run redis in protected mode
Port 6379 # Specify the port that the redis listens on.
Tcp-backlog 511
Timeout 0 # How long does the client idle when it is idle? When it is 0, it is turned off.
Tcp-keepalive 300 # tcp mode maximum allowed number of connections
Daemonize no # Whether to run as a daemon
Supervised no # Do not use the monitor tree
Pidfile /var/run/redis_6379.pid # redis save pid file at runtime
Loglevel verbose # Log file recording mode, default is standard output
Logfile /var/log/redis/redis.log # redis log file save path
Databases 16 # The number of redis databases saved in the system
Save 900 1 # redis has 1 update operation within 900 seconds, save the data to the file to save
Save 300 10 # redis synchronizes data to file when there are 10 update operations in 300 seconds
Save 60 10000 # redis synchronizes data to a file when there are 10,000 update operations in 60 seconds
Stop-writes-on-bgsave-error yes
Rdbcompression yes # Whether the data in redis is started when saving to the local database, the default is yes
Rdbchecksum yes
Dbfilename dump.rdb # filename of the local database
Dir /var/lib/redis # path to the 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 during each update operation
Appendfilename "appendonly.aof" # Specify the file name of the update log
Appendfsync everysec # Synchronize the data in redis to the local file once every 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 # Enter the redis interactive interface
127.0.0.1:6379>


As you can see, we've entered the Redis interactive environment.


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.python3 operating Redis


To open a Redis profile in a Linux system/etc/redis.conf



Replace the61line with the revised paragraph


bind 0.0.0.0


Indicates that all hosts can connect



Then restart Redis for the configuration file to take effect


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


In a Windows system, install the Redis module


pip3 install redis


Newredis_test.pyfile, file content is


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 Result:


The k1 value read for the first time: b‘v1‘
The k2 value read for the first time: b‘v2‘
Get the value of k1 for the second time. None
Get the value of k2 for the second time. None
B‘v3’


Get the K1 and K2 values again at the Linux command prompt




Because the values of K1 and K2 have been removed in redis_test.py, the obtained value is none.


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


Python3 operating Redis


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.