Skip to content

Instantly share code, notes, and snippets.

@seliger
Last active December 2, 2017 23:35
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
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