mirror of
https://github.com/spotipy-dev/spotipy.git
synced 2026-06-19 01:03:53 +00:00
Add RedisCacheHandler (#747)
* add RedisCacheHandler * add the explanation for customized chache handlers * update the dependency and chengelog
This commit is contained in:
parent
0a9270f385
commit
7d23fc3277
@ -7,7 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
## Unreleased
|
## Unreleased
|
||||||
|
|
||||||
// Add your changes here and then delete this line
|
### Added
|
||||||
|
* Added `RedisCacheHandler`, a cache handler that stores the token info in Redis.
|
||||||
|
|
||||||
## [2.19.0] - 2021-08-12
|
## [2.19.0] - 2021-08-12
|
||||||
|
|
||||||
|
|||||||
@ -231,6 +231,11 @@ The custom cache handler would need to be a class that inherits from the base
|
|||||||
cache handler ``CacheHandler``. The default cache handler ``CacheFileHandler`` is a good example.
|
cache handler ``CacheHandler``. The default cache handler ``CacheFileHandler`` is a good example.
|
||||||
An instance of that new class can then be passed as a parameter when
|
An instance of that new class can then be passed as a parameter when
|
||||||
creating ``SpotifyOAuth``, ``SpotifyPKCE`` or ``SpotifyImplicitGrant``.
|
creating ``SpotifyOAuth``, ``SpotifyPKCE`` or ``SpotifyImplicitGrant``.
|
||||||
|
The following handlers are available and defined in the URL above.
|
||||||
|
- ``CacheFileHandler``
|
||||||
|
- ``MemoryCacheHandler``
|
||||||
|
- ``DjangoSessionCacheHandler``
|
||||||
|
- ``RedisCacheHandler``
|
||||||
|
|
||||||
Feel free to contribute new cache handlers to the repo.
|
Feel free to contribute new cache handlers to the repo.
|
||||||
|
|
||||||
|
|||||||
1
setup.py
1
setup.py
@ -26,6 +26,7 @@ setup(
|
|||||||
author_email="paul@echonest.com",
|
author_email="paul@echonest.com",
|
||||||
url='https://spotipy.readthedocs.org/',
|
url='https://spotipy.readthedocs.org/',
|
||||||
install_requires=[
|
install_requires=[
|
||||||
|
'redis>=3.5.3',
|
||||||
'requests>=2.25.0',
|
'requests>=2.25.0',
|
||||||
'six>=1.15.0',
|
'six>=1.15.0',
|
||||||
'urllib3>=1.26.0'
|
'urllib3>=1.26.0'
|
||||||
|
|||||||
@ -1,4 +1,9 @@
|
|||||||
__all__ = ['CacheHandler', 'CacheFileHandler', 'DjangoSessionCacheHandler', 'MemoryCacheHandler']
|
__all__ = [
|
||||||
|
'CacheHandler',
|
||||||
|
'CacheFileHandler',
|
||||||
|
'DjangoSessionCacheHandler',
|
||||||
|
'MemoryCacheHandler',
|
||||||
|
'RedisCacheHandler']
|
||||||
|
|
||||||
import errno
|
import errno
|
||||||
import json
|
import json
|
||||||
@ -6,6 +11,8 @@ import logging
|
|||||||
import os
|
import os
|
||||||
from spotipy.util import CLIENT_CREDS_ENV_VARS
|
from spotipy.util import CLIENT_CREDS_ENV_VARS
|
||||||
|
|
||||||
|
from redis import RedisError
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@ -138,3 +145,32 @@ class DjangoSessionCacheHandler(CacheHandler):
|
|||||||
self.request.session['token_info'] = token_info
|
self.request.session['token_info'] = token_info
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warning("Error saving token to cache: " + str(e))
|
logger.warning("Error saving token to cache: " + str(e))
|
||||||
|
|
||||||
|
|
||||||
|
class RedisCacheHandler(CacheHandler):
|
||||||
|
"""
|
||||||
|
A cache handler that stores the token info in the Redis.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, redis):
|
||||||
|
"""
|
||||||
|
Parameters:
|
||||||
|
* redis: Redis object provided by redis-py library
|
||||||
|
(https://github.com/redis/redis-py)
|
||||||
|
"""
|
||||||
|
self.redis = redis
|
||||||
|
|
||||||
|
def get_cached_token(self):
|
||||||
|
token_info = None
|
||||||
|
try:
|
||||||
|
token_info = json.loads(self.redis.get('token_info'))
|
||||||
|
except RedisError as e:
|
||||||
|
logger.warning('Error getting token from cache: ' + str(e))
|
||||||
|
|
||||||
|
return token_info
|
||||||
|
|
||||||
|
def save_token_to_cache(self, token_info):
|
||||||
|
try:
|
||||||
|
self.redis.set('token_info', json.dumps(token_info))
|
||||||
|
except RedisError as e:
|
||||||
|
logger.warning('Error saving token to cache: ' + str(e))
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user