cache.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. __filename__ = "cache.py"
  2. __author__ = "Bob Mottram"
  3. __license__ = "AGPL3+"
  4. __version__ = "1.1.0"
  5. __maintainer__ = "Bob Mottram"
  6. __email__ = "bob@freedombone.net"
  7. __status__ = "Production"
  8. import os
  9. import datetime
  10. from utils import loadJson
  11. from utils import saveJson
  12. from utils import getFileCaseInsensitive
  13. def storePersonInCache(baseDir: str, personUrl: str,
  14. personJson: {}, personCache: {}) -> None:
  15. """Store an actor in the cache
  16. """
  17. currTime = datetime.datetime.utcnow()
  18. personCache[personUrl] = {
  19. "actor": personJson,
  20. "timestamp": currTime.strftime("%Y-%m-%dT%H:%M:%SZ")
  21. }
  22. if not baseDir:
  23. return
  24. # store to file
  25. if os.path.isdir(baseDir+'/cache/actors'):
  26. cacheFilename = baseDir + '/cache/actors/' + \
  27. personUrl.replace('/', '#')+'.json'
  28. if not os.path.isfile(cacheFilename):
  29. saveJson(personJson, cacheFilename)
  30. def getPersonFromCache(baseDir: str, personUrl: str, personCache: {}) -> {}:
  31. """Get an actor from the cache
  32. """
  33. # if the actor is not in memory then try to load it from file
  34. loadedFromFile = False
  35. if not personCache.get(personUrl):
  36. cacheFilename = baseDir + '/cache/actors/' + \
  37. personUrl.replace('/', '#')+'.json'
  38. if os.path.isfile(getFileCaseInsensitive(cacheFilename)):
  39. personJson = loadJson(getFileCaseInsensitive(cacheFilename))
  40. if personJson:
  41. storePersonInCache(baseDir, personUrl, personJson, personCache)
  42. loadedFromFile = True
  43. if personCache.get(personUrl):
  44. if not loadedFromFile:
  45. # update the timestamp for the last time the actor was retrieved
  46. currTime = datetime.datetime.utcnow()
  47. currTimeStr = currTime.strftime("%Y-%m-%dT%H:%M:%SZ")
  48. personCache[personUrl]['timestamp'] = currTimeStr
  49. return personCache[personUrl]['actor']
  50. return None
  51. def expirePersonCache(personCache: {}):
  52. """Expires old entries from the cache in memory
  53. """
  54. currTime = datetime.datetime.utcnow()
  55. removals = []
  56. for personUrl, cacheJson in personCache.items():
  57. cacheTime = datetime.datetime.strptime(cacheJson['timestamp'],
  58. "%Y-%m-%dT%H:%M:%SZ")
  59. daysSinceCached = (currTime - cacheTime).days
  60. if daysSinceCached > 2:
  61. removals.append(personUrl)
  62. if len(removals) > 0:
  63. for personUrl in removals:
  64. del personCache[personUrl]
  65. print(str(len(removals)) + ' actors were expired from the cache')
  66. def storeWebfingerInCache(handle: str, wf, cachedWebfingers: {}) -> None:
  67. """Store a webfinger endpoint in the cache
  68. """
  69. cachedWebfingers[handle] = wf
  70. def getWebfingerFromCache(handle: str, cachedWebfingers: {}) -> {}:
  71. """Get webfinger endpoint from the cache
  72. """
  73. if cachedWebfingers.get(handle):
  74. return cachedWebfingers[handle]
  75. return None