Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added MemcacheCacheHandler #1042

Merged
merged 5 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased

### Added

- Added `MemcacheCacheHandler`, a cache handler that stores the token info using pymemcache.
- Added support for audiobook endpoints: get_audiobook, get_audiobooks, and get_audiobook_chapters.
- Added integration tests for audiobook endpoints.
- Removed `python 2.7` from GitHub Actions CI workflow. Python v2.7 reached end of life support and is no longer supported by Ubuntu 20.04.
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"redis<4.0.0;python_version<'3.4'",
"requests>=2.25.0",
"six>=1.15.0",
"urllib3>=1.26.0"
"urllib3>=1.26.0",
"pymemcache>=3.5.2"
],
tests_require=test_reqs,
extras_require=extra_reqs,
Expand Down
33 changes: 32 additions & 1 deletion spotipy/cache_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
'DjangoSessionCacheHandler',
'FlaskSessionCacheHandler',
'MemoryCacheHandler',
'RedisCacheHandler']
'RedisCacheHandler',
'MemcacheCacheHandler']

import errno
import json
Expand All @@ -13,6 +14,7 @@
from spotipy.util import CLIENT_CREDS_ENV_VARS

from redis import RedisError
from pymemcache import MemcacheError

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -208,3 +210,32 @@ def save_token_to_cache(self, token_info):
self.redis.set(self.key, json.dumps(token_info))
except RedisError as e:
logger.warning('Error saving token to cache: ' + str(e))


class MemcacheCacheHandler(CacheHandler):
"""A Cache handler that stores the token info in Memcache using the pymemcache client
"""
def __init__(self, memcache, key=None) -> None:
"""
Parameters:
* memcache: memcache client object provided by pymemcache
(https://pymemcache.readthedocs.io/en/latest/getting_started.html)
* key: May be supplied, will otherwise be generated
(takes precedence over `token_info`)
"""
self.memcache = memcache
self.key = key if key else 'token_info'

def get_cached_token(self):
try:
token_info = self.memcache.get(self.key)
if token_info:
return json.loads(token_info.decode())
except MemcacheError as e:
logger.warning('Error getting token from cache' + str(e))

def save_token_to_cache(self, token_info):
try:
self.memcache.set(self.key, json.dumps(token_info))
except MemcacheError as e:
logger.warning('Error saving token to cache' + str(e))
Loading