[PYTHON] Record user last access time with Redis

I used to record the user's last access time in the DB, but I want to update it frequently, but it becomes a bottleneck in the DB, so I replaced it with redis.

advantage

lastaccess.py


from datetime import datetime

import pytz
import redis


class UserLastAccess(object):

    def __init__(self, user_id):
        self._user_id = user_id
        self._client = redis.StrictRedis()
        self._key = self.__class__.__name__

    @property
    def accessed_at(self):
        timestamp = self._client.zscore(self._key, self._user_id)
        if timestamp:
            d = datetime.fromtimestamp(int(timestamp))
            return pytz.timezone('Asia/Tokyo').normalize(pytz.utc.localize(d))

        return None

    def touch(self):
        now = pytz.utc.localize(datetime.utcnow())
        timestamp = now.strftime('%s')

        self._client.zadd(self._key, timestamp, self._user_id)

        return pytz.timezone('Asia/Tokyo').normalize(now)


class LastAccessedAtMixin(object):

    @property
    def user_last_access(self):
        return UserLastAccess(self.id)

    @property
    def last_accessed_at(self):
        return self.user_last_access.accessed_at

    def touch(self):
        return self.user_last_access.touch()


class User(LastAccessedAtMixin):

    def __init__(self, user_id):
        self.id = user_id

Example of use

ex


user = User('1000')
user.touch()  # => datetime.datetime(2014, 10, 6, 21, 55, 27, 654918, tzinfo=<DstTzInfo 'Asia/Tokyo' JST+9:00:00 STD>)
user.last_accessed_at  # => datetime.datetime(2014, 10, 6, 21, 55, 27, tzinfo=<DstTzInfo 'Asia/Tokyo' JST+9:00:00 STD>)

Recommended Posts

Record user last access time with Redis
Django: Record User Agent and manage with Admin
mongodb access with pymongo
I tried multiple user management (granting access rights) with ansible