From 41980ff27ef0e41edf6eada03bf0c04e5e52d195 Mon Sep 17 00:00:00 2001
From: Inso <insomniak.fr@gmail.com>
Date: Thu, 19 Feb 2015 21:00:15 +0100
Subject: [PATCH] Added caching of individual blocks

---
 src/cutecoin/core/app.py       | 21 +++++++++++++++++++++
 src/cutecoin/core/community.py | 30 +++++++++++++++++++++++++++++-
 2 files changed, 50 insertions(+), 1 deletion(-)

diff --git a/src/cutecoin/core/app.py b/src/cutecoin/core/app.py
index 8cc937be..47ef017f 100644
--- a/src/cutecoin/core/app.py
+++ b/src/cutecoin/core/app.py
@@ -88,6 +88,18 @@ class Application(object):
             self.accounts[account_name] = 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:
             wallet_path = os.path.join(config.parameters['home'],
                                         account.name, '__cache__', wallet.pubkey)
@@ -130,6 +142,15 @@ class Application(object):
                 data['version'] = __version__
                 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):
         with tarfile.open(file, "r") as tar:
             path = os.path.join(config.parameters['home'],
diff --git a/src/cutecoin/core/community.py b/src/cutecoin/core/community.py
index b1b1a9f9..eeb2fb3d 100644
--- a/src/cutecoin/core/community.py
+++ b/src/cutecoin/core/community.py
@@ -22,9 +22,31 @@ class Cache():
         self.community = community
         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):
         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={}):
         cache_key = (hash(request),
@@ -116,6 +138,12 @@ class Community(object):
         community = cls(currency, peers)
         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):
         return self.currency
 
-- 
GitLab