Skip to content

Commit

Permalink
Added MemcacheCacheHandler (#1042)
Browse files Browse the repository at this point in the history
* Added MemcacheCacheHandler

* Import MemcacheError where used

* Update index.rst

---------

Co-authored-by: Stéphane Bruckert <[email protected]>
  • Loading branch information
andrewcara and stephanebruckert authored May 30, 2024
1 parent 913ae57 commit 62a27a2
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
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: 3 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,17 @@ cache handler ``CacheHandler``. The default cache handler ``CacheFileHandler`` i
An instance of that new class can then be passed as a parameter when
creating ``SpotifyOAuth``, ``SpotifyPKCE`` or ``SpotifyImplicitGrant``.
The following handlers are available and defined in the URL above.

- ``CacheFileHandler``
- ``MemoryCacheHandler``
- ``DjangoSessionCacheHandler``
- ``FlaskSessionCacheHandler``
- ``RedisCacheHandler``
- ``MemcacheCacheHandler``: install with dependency using ``pip install "spotipy[pymemcache]"``

Feel free to contribute new cache handlers to the repo.


Examples
=======================

Expand Down
9 changes: 7 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@
'mock==2.0.0'
]

memcache_cache_reqs = [
'pymemcache>=3.5.2'
]

extra_reqs = {
'test': test_reqs
'test': test_reqs,
'memcache': memcache_cache_reqs
}

setup(
Expand All @@ -25,7 +30,7 @@
},
python_requires='>3.8',
install_requires=[
"redis>=3.5.3",
"redis>=3.5.3", # TODO: Move to extras_require in v3
"requests>=2.25.0",
"urllib3>=1.26.0"
],
Expand Down
34 changes: 33 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 Down Expand Up @@ -208,3 +209,34 @@ 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):
from pymemcache import MemcacheError
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):
from pymemcache import MemcacheError
try:
self.memcache.set(self.key, json.dumps(token_info))
except MemcacheError as e:
logger.warning('Error saving token to cache' + str(e))

0 comments on commit 62a27a2

Please sign in to comment.