Create a gist now

Instantly share code, notes, and snippets.

Embed
Using Redis to cache the Todoist Python API
import todoist
import json
import redis
import sys
def _monkey_read_cache(self):
if not self.redis:
return
try:
self._update_state(json.loads(self.redis.get(self.token + '.json').decode('utf-8')))
self.sync_token = self.redis.get(self.token + '.sync').decode('utf-8')
except AttributeError:
print('[WARN] - There was no data to decode (likely a cache miss).')
return
except:
print("Unexpected error:", sys.exc_info()[0])
raise
def state_default(obj):
return obj.data
def _monkey_write_cache(self):
if not self.redis:
return
self.redis.set(self.token + '.json', json.dumps(self.state, default=state_default))
self.redis.set(self.token + '.sync', self.sync_token)
# Inject the functions
todoist.TodoistAPI._read_cache = _monkey_read_cache
todoist.TodoistAPI._write_cache = _monkey_write_cache
# Inject a working Redis session into the TodoistAPI instance
todoist.TodoistAPI.redis = redis.StrictRedis(host="localhost", port=6379, db=0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment