import redis_collections
import threading
import time
# noinspection PyUnresolvedReferences
import __main__


class RedisDict(redis_collections.Dict):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.die = False
        self.thread = threading.Thread(target=self.update_loop, daemon=True, name=kwargs['key'])
        self.thread.start()
        self.rthread = threading.Thread(target=self.refresh_loop, daemon=True, name=kwargs['key'])
        self.rthread.start()
        self.prev = None
        db = str(self.redis.connection_pool.connection_kwargs['db'])
        self.pubsub_format = 'liara.{}.{}'.format(db, kwargs['key'])

    def update_loop(self):
        time.sleep(2)
        while not self.die:
            if self.prev != str(self.cache):
                self.prev = str(self.cache)
                self.sync()
                self.redis.publish(self.pubsub_format, 'update')
                time.sleep(0.01)
            else:
                time.sleep(0.01)

    def refresh_loop(self):
        time.sleep(2)
        pubsub = self.redis.pubsub()
        pubsub.subscribe([self.pubsub_format])
        for message in pubsub.listen():
            if message['type'] == 'message':
                self.cache.clear()
                self.cache = dict(self)
                self.prev = str(self.cache)


class dataIO:
    @staticmethod
    def save_json(filename, content):
        pass  # "oops"

    @staticmethod
    def load_json(filename):
        return RedisDict(key=filename, redis=__main__.redis_conn, writeback=True)

@@ -47,16 +47,28 @@
 rt time%0A
+import json%0A
 # noinsp
@@ -919,16 +919,90 @@
 mat,
+ json.dumps(%7B%0A                    'shard': __main__.liara.shard_id, 'msg':
  'update
 ')%0A 
@@ -997,17 +997,20 @@
  'update
-'
+!'%7D)
 )%0A      
@@ -1293,17 +1293,17 @@
 'type'%5D 
-=
+!
 = 'messa
@@ -1307,24 +1307,249 @@
 ssage':%0A    
+            continue%0A            data = json.loads(message%5B'data'%5D.decode())%0A            if data%5B'shard'%5D == __main__.liara.shard_id:%0A                continue%0A            if data%5B'msg'%5D != 'update!':%0A                continue%0A
             
@@ -1567,20 +1567,16 @@
 clear()%0A
-    
         
@@ -1603,20 +1603,16 @@
 t(self)%0A
-    
         

Optimize and improve dataIO