예제 #1
0
파일: redis.py 프로젝트: JKArena/pulsing
class Redis():
    INVITE_EXPIRATION = 300

    def __init__(self,
                 host='pulsing.jhk.org',
                 port=6379,
                 password='******',
                 db=0,
                 **kwargs):
        super().__init__()
        self.logger = logging.getLogger(__name__)
        self.__client = StrictRedis(host=host,
                                    port=port,
                                    password=password,
                                    db=0,
                                    **kwargs)

    def storeInvitation(self, to_user_id, from_user_id, invitation_id,
                        invitationType):
        expiration = datetime.utcnow() + timedelta(
            seconds=Redis.INVITE_EXPIRATION)
        self.__client.setex(invitation_id, '1', Redis.INVITE_EXPIRATION)
        self.__client.sadd(
            'INVITATIONS_' + to_user_id,
            json.dumps(
                getInvitation(from_user_id, invitationType, invitation_id,
                              expiration.timestamp() * 1000.0)))

    def removeInvitation(self, user_id, invitation_id):
        s_invitations = self.__client.smembers('INVITATIONS_' + user_id)
        self.logger.debug('removeInvitation fetched - %s ', s_invitations)

        invitations = json.loads(s_invitations)
        self.logger.debug('removeInvitation parsed - %s ', invitations)

        invitation = [
            invite for invite in invitations
            if (invite['invitationId'] == invitation_id)
        ]
        self.__client.delete(invitation_id)

        return invitation

    @property
    def client(self):
        return self.__client

    def __eq__(self, other):
        return self.__client == other.__client

    def __str__(self):
        return self.__client.__str__()

    def __hash__(self):
        return self.__client.__hash__()
예제 #2
0
파일: redis.py 프로젝트: JHKTruth/pulsing
class Redis():
    INVITE_EXPIRATION = 300
    
    def __init__(self, host='pulsing.jhk.org', port=6379, password='******', db=0, **kwargs):
        super().__init__()
        self.logger = logging.getLogger(__name__)
        self.__client = StrictRedis(host=host, port=port, password=password, db=0, **kwargs)
    
    def storeInvitation(self, to_user_id, from_user_id, invitation_id, invitationType):
        expiration = datetime.utcnow() + timedelta(seconds=Redis.INVITE_EXPIRATION)
        self.__client.setex(invitation_id, '1', Redis.INVITE_EXPIRATION)
        self.__client.sadd('INVITATIONS_'+to_user_id,
                           json.dumps(getInvitation(from_user_id, invitationType, invitation_id, expiration.timestamp() * 1000.0)))

    def removeInvitation(self, user_id, invitation_id):
        s_invitations = self.__client.smembers('INVITATIONS_'+user_id)
        self.logger.debug('removeInvitation fetched - %s ', s_invitations)
        
        invitations = json.loads(s_invitations)
        self.logger.debug('removeInvitation parsed - %s ', invitations)
        
        invitation = [invite for invite in invitations if (invite['invitationId'] == invitation_id)]
        self.__client.delete(invitation_id)
        
        return invitation
        
    @property
    def client(self):
        return self.__client

    def __eq__(self, other):
        return self.__client == other.__client

    def __str__(self):
        return self.__client.__str__()

    def __hash__(self):
        return self.__client.__hash__()