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})
def test_session_expire(self): KEY = random_str() VALUE = random_str() EXPIRE_TIME = 2 SessionDATcp = ServiceMgrCacher().get_connection(ST_SESSION_DA, rdm_type=RT_HASH_RING, rdm_param=KEY) self.assertEqual(SessionDATcp.ping(), "ping response!") self.assertTrue(user_session_insert(SessionDATcp, {KEY: [VALUE, EXPIRE_TIME]})) time.sleep(EXPIRE_TIME+1) self.assertTrue(user_session_select(SessionDATcp, [KEY]) == {KEY: None})
def check_expire_access_token(access_token): """ check access token whether is validate; if it is, reset it's expire time :param access_token: :return: """ token_user_key = key_maker.mk_token_username_key(access_token) session_tcp = ServiceMgrCacher().get_connection(US_SESSION_DA, RT_HASH_RING, token_user_key) result = user_session_select(session_tcp, [token_user_key]) validate = True if result[token_user_key] else False if validate: # reset expire time params = {token_user_key: setting.ACCESS_TOKEN_EXPIRE} user_session_expire(session_tcp, params) return validate
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