neo4j-contrib / neomodel

An Object Graph Mapper (OGM) for the Neo4j graph database.
https://neomodel.readthedocs.io
MIT License
936 stars 231 forks source link

Test integration of cachetools #808

Open mariusconjeaud opened 3 months ago

mariusconjeaud commented 3 months ago

General idea would be :

from cachetools import LRUCache, cached
from neomodel import StructuredNode

# Setup cache
cache = LRUCache(maxsize=100)  # Adjust size based on expected workload and available memory

class CachableStructuredNode(StructuredNode):
    @classmethod
    @cached(cache)
    def get(cls, id):
        return super(CachableStructuredNode, cls).get(id=id)

    def save(self):
        # Invalidate cache when the node is updated
        cache.pop(self.id, None)
        super(CachableStructuredNode, self).save()

    def delete(self):
        # Invalidate cache when the node is deleted
        cache.pop(self.id, None)
        super(CachableStructuredNode, self).delete()

Then, more complete solutions like redis or memcached could come later