cache.py 3.1 KB

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