| commit 51: | f63b3bbd605f |
| parent 50: | d1dd19a26f3c |
| branch: | default |
Added key formatter function for cache keys
Changed (Δ476 bytes):
raw changeset »
spotimeta/__init__.py (9 lines added, 1 lines removed)
Up to file-list spotimeta/__init__.py:
| … | … | @@ -96,15 +96,21 @@ class Metadata(object): |
96 |
96 |
.. attribute:: cache |
97 |
97 |
.. attribute:: timeout |
98 |
98 |
.. attribute:: uesr_agent |
99 |
.. attribute:: key_formatter : function that can change the format and |
|
100 |
type of the cache key if neccesary. By default it just |
|
101 |
returns what it is given unmodified. Changing this can |
|
102 |
be useful if the cache backend does not support unicode |
|
103 |
keys, like for instance shelves from the shelve module. |
|
99 |
104 |
|
100 |
105 |
|
101 |
106 |
""" |
102 |
107 |
|
103 |
108 |
def __init__(self, cache=None, rate=10, timeout=None, user_agent=None): |
104 |
self.cache = cache |
|
109 |
self.cache = cache |
|
105 |
110 |
self.rate = rate # not implemented yet |
106 |
111 |
self.timeout = timeout |
107 |
112 |
self.user_agent = user_agent or USER_AGENT |
113 |
self.key_formatter = lambda x: x |
|
108 |
114 |
self._timeout_supported = True |
109 |
115 |
self._port = "80" |
110 |
116 |
self._host = "ws.spotify.com" |
| … | … | @@ -274,6 +280,7 @@ class Metadata(object): |
274 |
280 |
""" |
275 |
281 |
entry = None |
276 |
282 |
if self.cache is not None: |
283 |
key = self.key_formatter(key) |
|
277 |
284 |
entry = self.cache.get(key) |
278 |
285 |
|
279 |
286 |
return entry or (None, 0, 0) |
| … | … | @@ -281,6 +288,7 @@ class Metadata(object): |
281 |
288 |
def _cache_put(self, key, value, modified, expires): |
282 |
289 |
"""Inverse of _cache_put""" |
283 |
290 |
if self.cache is not None: |
291 |
key = self.key_formatter(key) |
|
284 |
292 |
self.cache[key] = value, modified, expires |
285 |
293 |
|
286 |
294 |
# This is an instance of the metadata module used for module level |
