当前位置:网站首页>Django uses redis to store sessions starting from 0

Django uses redis to store sessions starting from 0

2022-06-13 05:18:00 Puff o

1. Configure the server side redis

1.1 Server installation redis

  1. Please refer to https://www.cnblogs.com/architectforest/p/12325230.html
    Blogger installed redis The version number is 6.x

1.2 modify redis.conf The configuration file

  1. modify redis.config file , If it is the above installation steps , The file path is /usr/local/soft/redis6/conf/redis.conf
    command :vim /usr/local/soft/redis6/conf/redis.conf
    1.1 Set listening to all ip, take bind 127.0.0.1 Change it to bind 0.0.0.0
    1.2. Set up redis Running in the background , take demonize no Change it to demonize yes
    1.3. Turn off protection , take protected-mode yes Change it to protected-mode no
    1.4. Add password , Add a field to the configuration file requirepass 123456 The code here is 123456, Change to the required password .

1.3 Firewall configuration

  1. Turn on 6379 port ( Corresponds to the port in the configuration file ), command firewall-cmd --zone=public --add-port=6379/tcp --permanent
  2. service iptables restart , command firewall-cmd --reload
    If it is aliyun server , You need to add firewall rules on the Alibaba cloud console , Turn on tcp Mode 6379 port
  3. restart redis, command /usr/local/soft/redis6/bin/redis-server /usr/local/soft/redis6/conf/redis.conf
    If the systemctl, You can start... With this command systemctl restart redis

1.4 In the local windows Test the connection from the command line

  1. Search for cmd, Carry out orders redis-cli -h 47.104.*.** -p 6379 -a 123456
    -h Then fill in the server's IP Address ,-p Then fill in the server redis Listening port ,-a Fill in after redis Password
    After successful connection, you can proceed to the next step .

2. To configure Django

2.1 Install the required dependency packages

  1. If you use pycharm Directly install in the project interpreter in settings django-redis
    If you use other tools , Install with command pip install django-redis

2.2 To configure Django Medium settings

  1. stay settings.py In file , Add the following fields
  2. SESSION_CACHE_ALIAS Medium default And CACHES Medium default Corresponding .
  3. It's only preserved here session, So you don't need to set decode_response, If you want to save other data to redis, This field is set according to business needs .

 Insert picture description here
 Insert picture description here

2.3 test

  1. The business logic submits a for the front end pos t Request login , then django Use session Save to redis Record login status in .

  2. stay views For example, write a login request in the view . Front end and routing settings There's nothing more to be said here .
     Insert picture description here

  3. session It is better to write a global variable separately , Easy to modify together .

  4. All you need to do is write session The syntax of can be automatically saved to redis in . The deletion syntax is the same ,django It comes with an expired purge session The order of django-admin clearsessions

  5. views Realize the logout function in . If you need to permanently save records, you need additional settings redis.
     Insert picture description here

  6. Send a login request through the front end , And the login is successful , Let's go to the server and have a look redis Whether the file was saved successfully .
    6.1 Connect redis, command /usr/local/soft/redis6/bin/redis-cli -a 123456 The password is also required for the server local connection , Otherwise, you cannot select a library .
    6.2 Select Library ,django in settings Save the settings to 1 Signal library , choice 1 Signal library , command select 1
    Show ok It means success .
    6.3 View all key, command keys * The newly stored session. Need to be in session Within the valid time of , Otherwise, you will not be able to view .
     Insert picture description here

  7. Only this and nothing more , This article has been completed .

原网站

版权声明
本文为[Puff o]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280514132726.html