Exemplo n.º 1
0
    def generate_access_token(user_name):
        """
        根据用户名,产生新的访问token,并保持相关的访问key到数据访问层
        :param user_name: 用户名
        :return:字符串
        """
        # 产生新access_token
        new_access_token = key_maker.mk_access_token()
        token_user_key = key_maker.mk_token_username_key(new_access_token)
        session_tcp = ServiceMgrCacher().get_connection(US_SESSION_DA, RT_HASH_RING, token_user_key)

        # 删除以前的access_token
        user_token_key = key_maker.mk_username_token_key(user_name)
        user_session_info = user_session_select(session_tcp, [user_token_key])
        if user_session_info:
            old_access_token = user_session_info[user_token_key]
            if old_access_token:
                user_session_delete(session_tcp, [key_maker.mk_token_username_key(old_access_token)])

        # 存储新的access_token
        params = {token_user_key: [user_name, setting.ACCESS_TOKEN_EXPIRE],
                  user_token_key: [new_access_token]}
        user_session_insert(session_tcp, params)
        return new_access_token
Exemplo n.º 2
0
    def test_session_insert_select_delete_update(self):
        KEY1 = random_str()
        VALUE1 = random_str()
        EXPIRE_TIME1 = 200

        SessionDATcp = ServiceMgrCacher().get_connection(ST_SESSION_DA,
                                                      rdm_type=RT_HASH_RING,
                                                      rdm_param=KEY1)
        self.assertEqual(SessionDATcp.ping(), "ping response!")

        params = {KEY1: [VALUE1, EXPIRE_TIME1]}
        self.assertTrue(user_session_insert(SessionDATcp, params))

        self.assertTrue(user_session_select(SessionDATcp, [KEY1]) == \
                        {KEY1: VALUE1})

        params = {KEY1: EXPIRE_TIME1}
        self.assertTrue(user_session_expire(SessionDATcp, params))

        self.assertTrue(user_session_delete(SessionDATcp, [KEY1]))

        self.assertTrue(user_session_select(SessionDATcp, [KEY1]) == \
                        {KEY1: None})