import redis_collections
import threading
import time
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.prev = None

    def update_loop(self):
        time.sleep(2)
        while not self.die:
            if self.prev != repr(self):
                self.prev = repr(self)
                self.sync()
                time.sleep(0.1)
            else:
                self.cache.clear()
                time.sleep(0.1)


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)

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)

Make config sync more efficient
Make config sync more efficient

  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 != repr(self):
+             if self.prev != str(self.cache):
-                 self.prev = repr(self)
+                 self.prev = str(self.cache)
                  self.sync()
+                 self.redis.publish(self.pubsub_format, 'update')
-                 time.sleep(0.1)
+                 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()
-                 time.sleep(0.1)
+                 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)
  
Make config sync more efficient
## Code Before:
import redis_collections
import threading
import time
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.prev = None

    def update_loop(self):
        time.sleep(2)
        while not self.die:
            if self.prev != repr(self):
                self.prev = repr(self)
                self.sync()
                time.sleep(0.1)
            else:
                self.cache.clear()
                time.sleep(0.1)


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)

## Instruction:
Make config sync more efficient
## Code After:
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)
