Skip to content

Commit

Permalink
Need to be able to list id-specific list of objects, too
Browse files Browse the repository at this point in the history
  • Loading branch information
xeroc committed Dec 21, 2018
1 parent ea0b855 commit da45764
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions graphenecommon/blockchainobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ def _store_item(self, key=None):

def _store_items(self, key=None):
key = key or self.__class__.__name__
self._cache[key] = list(self)
if key in self._cache:
self._cache[key].extend(list(self))
else:
self._cache[key] = list(self)
self._fetched = True

def incached(self, id):
Expand Down Expand Up @@ -151,30 +154,32 @@ def refresh(self, *args, **kwargs):

def __init__(self, *args, **kwargs):
Caching.__init__(self, *args, **kwargs)
key = self._cache_key
# Some lists are specific to some key value that is then provided as
# first argument
if len(args) > 0:
key = self._cache_key(args[0])
else:
key = self._cache_key()
if self.incached(key):
list.__init__(self, self.getfromcache(key))
else:
if kwargs.get("refresh", True):
self.refresh(*args, **kwargs)

@property
def _cache_key(self):
key = self.__class__.__name__
key += self.identifier or ""
return key
def _cache_key(self, key=""):
if key:
# We add the key to the index
return "{}-{}".format(self.__class__.__name__, key)
else:
return self.__class__.__name__

def store(self, data, *args, **kwargs):
def store(self, data, key=None, *args, **kwargs):
""" Cache the list
:param list data: List of objects to cache
"""
list.__init__(self, data)
# We do not allow to provide a separate key as we do for
# a BlockchainObject (without s). The reson is that we store
# lists and do not need to be able to query for names, ids, or
# other objects
self._store_items(self._cache_key)
self._store_items(self._cache_key(key))

@classmethod
def cache_objects(cls, data, key=None):
Expand Down Expand Up @@ -271,7 +276,7 @@ def cache_object(cls, data, key=None):

@classmethod
def _import(cls, data, key=None):
c = cls(key, refresh=False)
c = cls(data, refresh=False)
c.store(data, key)
return c

Expand Down

0 comments on commit da45764

Please sign in to comment.