接收长度为0的Map的“hmset”错误

mum43rcc  于 2021-06-09  发布在  Redis
关注(0)|答案(1)|浏览(481)

我想在redis数据集上存储会话数据。我已经准备好了 SESSION_ENGINE = 'redis'settings.py .
redis.py的代码


# redis.py

from django.contrib.sessions.backends.base import SessionBase
from django.utils.functional import cached_property
from redis import Redis

class SessionStore(SessionBase):
    @cached_property

    def _connection(self):
        return Redis(
            host='127.0.0.1',
            port='6379',
            db=0,
            decode_responses=True
        )

    def load(self):
        return self._connection.hgetall(self.session_key)

    def exists(self, session_key):
        return self._connection.exists(session_key)

    def create(self):
        # Creates a new session in the database.
        self._session_key = self._get_new_session_key()
        self.save(must_create=True)
        self.modified = True

    def save(self, must_create=False):
        # Saves the session data. If `must_create` is True,
        # creates a new session object. Otherwise, only updates
        # an existing object and doesn't create one.
        if self.session_key is None:
            return self.create()
        data = self._get_session(no_load=must_create)
        session_key = self._get_or_create_session_key()
        self._connection.hmset(session_key, data)
        self._connection.expire(session_key, self.get_expiry_age())

    def delete(self, session_key=None):
        # Deletes the session data under the session key.
        if session_key is None:
            if self.session_key is None:
                return
            session_key = self.session_key
        self._connection.delete(session_key)
    @classmethod

    def clear_expired(cls):
        # There is no need to remove expired sessions by hand
        # because Redis can do it automatically when
        # the session has expired.
        # We set expiration time in `save` method.
        pass

我正在接收长度Map为0的“hmset”,访问时出错http://localhost:django的8000/管理员。
拆卸后 SESSION_ENGINE='redis' 我没有收到这个错误。

wgxvkvu9

wgxvkvu91#

从redis文档:
根据redis 4.0.0,hmset被认为是不推荐使用的。请在新代码中使用hset。
我已经把这条线换了 save() 方法:

self._connection.hmset(session_key, data)

使用:

self._connection.hset(session_key, 'session_key', session_key, data)

在进行更改时,它按预期工作。

相关问题