cache.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. __filename__ = "cache.py"
  2. __author__ = "Bob Mottram"
  3. __license__ = "AGPL3+"
  4. __version__ = "0.0.1"
  5. __maintainer__ = "Bob Mottram"
  6. __email__ = "bob@freedombone.net"
  7. __status__ = "Production"
  8. import datetime
  9. def storePersonInCache(personUrl: str,personJson: {},personCache: {}) -> None:
  10. """Store an actor in the cache
  11. """
  12. currTime=datetime.datetime.utcnow()
  13. personCache[personUrl]={
  14. "actor": personJson,
  15. "timestamp": currTime.strftime("%Y-%m-%dT%H:%M:%SZ")
  16. }
  17. def storeWebfingerInCache(handle: str,wf,cachedWebfingers: {}) -> None:
  18. """Store a webfinger endpoint in the cache
  19. """
  20. cachedWebfingers[handle]=wf
  21. def getPersonFromCache(personUrl: str,personCache: {}) -> {}:
  22. """Get an actor from the cache
  23. """
  24. if personCache.get(personUrl):
  25. # how old is the cached data?
  26. currTime=datetime.datetime.utcnow()
  27. cacheTime= \
  28. datetime.datetime.strptime(personCache[personUrl]['timestamp'], \
  29. "%Y-%m-%dT%H:%M:%SZ")
  30. daysSinceCached=(currTime - cacheTime).days
  31. # return cached value if it has not expired
  32. if daysSinceCached <= 2:
  33. return personCache[personUrl]['actor']
  34. return None
  35. def getWebfingerFromCache(handle: str,cachedWebfingers: {}) -> {}:
  36. """Get webfinger endpoint from the cache
  37. """
  38. if cachedWebfingers.get(handle):
  39. return cachedWebfingers[handle]
  40. return None