mirror of
https://github.com/spotipy-dev/spotipy.git
synced 2026-06-19 09:13:53 +00:00
Allow to set custom key in RedisCacheHandler (#761)
* Allow to set custom key in RedisCacheHandler * Update CHANGELOG.md * Check for the existence of the key
This commit is contained in:
parent
0464f4f483
commit
08411b9031
@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
### Added
|
### Added
|
||||||
* Added `RedisCacheHandler`, a cache handler that stores the token info in Redis.
|
* Added `RedisCacheHandler`, a cache handler that stores the token info in Redis.
|
||||||
* Changed URI handling in `client.Spotify._get_id()` to remove qureies if provided by error.
|
* Changed URI handling in `client.Spotify._get_id()` to remove qureies if provided by error.
|
||||||
|
* Added a new parameter to `RedisCacheHandler` to allow custom keys (instead of the default `token_info` key)
|
||||||
|
|
||||||
## [2.19.0] - 2021-08-12
|
## [2.19.0] - 2021-08-12
|
||||||
|
|
||||||
|
|||||||
@ -152,18 +152,22 @@ class RedisCacheHandler(CacheHandler):
|
|||||||
A cache handler that stores the token info in the Redis.
|
A cache handler that stores the token info in the Redis.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, redis):
|
def __init__(self, redis, key=None):
|
||||||
"""
|
"""
|
||||||
Parameters:
|
Parameters:
|
||||||
* redis: Redis object provided by redis-py library
|
* redis: Redis object provided by redis-py library
|
||||||
(https://github.com/redis/redis-py)
|
(https://github.com/redis/redis-py)
|
||||||
|
* key: May be supplied, will otherwise be generated
|
||||||
|
(takes precedence over `token_info`)
|
||||||
"""
|
"""
|
||||||
self.redis = redis
|
self.redis = redis
|
||||||
|
self.key = key if key else 'token_info'
|
||||||
|
|
||||||
def get_cached_token(self):
|
def get_cached_token(self):
|
||||||
token_info = None
|
token_info = None
|
||||||
try:
|
try:
|
||||||
token_info = json.loads(self.redis.get('token_info'))
|
if self.redis.exists(self.key):
|
||||||
|
token_info = json.loads(self.redis.get(self.key))
|
||||||
except RedisError as e:
|
except RedisError as e:
|
||||||
logger.warning('Error getting token from cache: ' + str(e))
|
logger.warning('Error getting token from cache: ' + str(e))
|
||||||
|
|
||||||
@ -171,6 +175,6 @@ class RedisCacheHandler(CacheHandler):
|
|||||||
|
|
||||||
def save_token_to_cache(self, token_info):
|
def save_token_to_cache(self, token_info):
|
||||||
try:
|
try:
|
||||||
self.redis.set('token_info', json.dumps(token_info))
|
self.redis.set(self.key, json.dumps(token_info))
|
||||||
except RedisError as e:
|
except RedisError as e:
|
||||||
logger.warning('Error saving token to cache: ' + str(e))
|
logger.warning('Error saving token to cache: ' + str(e))
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user