Skip to content
Snippets Groups Projects
Commit 41980ff2 authored by inso's avatar inso
Browse files

Added caching of individual blocks

parent ddc30d28
No related branches found
No related tags found
No related merge requests found
...@@ -88,6 +88,18 @@ class Application(object): ...@@ -88,6 +88,18 @@ class Application(object):
self.accounts[account_name] = account self.accounts[account_name] = account
def load_cache(self, account): def load_cache(self, account):
for community in account.communities:
community_path = os.path.join(config.parameters['home'],
account.name, '__cache__',
community.currency)
if os.path.exists(community_path):
with open(community_path, 'r') as json_data:
data = json.load(json_data)
if 'version' in data and data['version'] == __version__:
community.load_cache(data)
else:
os.remove(community_path)
for wallet in account.wallets: for wallet in account.wallets:
wallet_path = os.path.join(config.parameters['home'], wallet_path = os.path.join(config.parameters['home'],
account.name, '__cache__', wallet.pubkey) account.name, '__cache__', wallet.pubkey)
...@@ -130,6 +142,15 @@ class Application(object): ...@@ -130,6 +142,15 @@ class Application(object):
data['version'] = __version__ data['version'] = __version__
json.dump(data, outfile, indent=4, sort_keys=True) json.dump(data, outfile, indent=4, sort_keys=True)
for community in account.communities:
community_path = os.path.join(config.parameters['home'],
account.name, '__cache__',
community.currency)
with open(community_path, 'w') as outfile:
data = community.jsonify_cache()
data['version'] = __version__
json.dump(data, outfile, indent=4, sort_keys=True)
def import_account(self, file, name): def import_account(self, file, name):
with tarfile.open(file, "r") as tar: with tarfile.open(file, "r") as tar:
path = os.path.join(config.parameters['home'], path = os.path.join(config.parameters['home'],
......
...@@ -22,9 +22,31 @@ class Cache(): ...@@ -22,9 +22,31 @@ class Cache():
self.community = community self.community = community
self.data = {} self.data = {}
def load_from_json(self, data):
self.data = {}
for entry in data['cache']:
key = entry['key']
cache_key = (key[0], key[1], key[2], key[3], key[4])
self.data[cache_key] = entry['value']
self.latest_block = data['latest_block']
def jsonify(self):
saved_requests = [hash(bma.blockchain.Block)]
data = {k: self.data[k] for k in self.data.keys()
if k[0] in saved_requests}
entries = []
for d in data:
entries.append({'key': d,
'value': data[d]})
return {'latest_block': self.latest_block,
'cache': entries}
def refresh(self): def refresh(self):
self.latest_block = self.community.current_blockid()['number'] self.latest_block = self.community.current_blockid()['number']
self.data = {} saved_requests = [hash(bma.blockchain.Block)]
self.data = {k: self.data[k] for k in self.data.keys()
if k[0] in saved_requests}
def request(self, request, req_args={}, get_args={}): def request(self, request, req_args={}, get_args={}):
cache_key = (hash(request), cache_key = (hash(request),
...@@ -116,6 +138,12 @@ class Community(object): ...@@ -116,6 +138,12 @@ class Community(object):
community = cls(currency, peers) community = cls(currency, peers)
return community return community
def load_cache(self, json_data):
self._cache.load_from_json(json_data)
def jsonify_cache(self):
return self._cache.jsonify()
def name(self): def name(self):
return self.currency return self.currency
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment