Exemplo n.º 1
0
 def get_redis_instance(self):
     try:
         if self.connection is None:
             self.connect()
         return self.connection
     except Exception as ex:
         raise CacheException(ErrorCodes.GENERAL_ERROR, str(ex), None)
Exemplo n.º 2
0
 def bytes_to_obj(self, bytes):
     try:
         str_binary = ''.join(chr(int(x, 2)) for x in bytes.split())
         obj = json.loads(str_binary)
         return obj
     except Exception as ex:
         raise CacheException(ErrorCodes.GENERAL_ERROR, str(ex), None)
Exemplo n.º 3
0
 def clear_cache(self):
     try:
         if settings.CACHE_ENABLED:
             return self.get_redis_instance().flushall()
         else:
             return True
     except Exception as ex:
         raise CacheException(ErrorCodes.GENERAL_ERROR, str(ex), None)
Exemplo n.º 4
0
 def invalidate_cache(self, key):
     try:
         if settings.CACHE_ENABLED:
             return self.get_redis_instance().delete(key)
         else:
             return True
     except Exception as ex:
         raise CacheException(ErrorCodes.GENERAL_ERROR, str(ex), None)
Exemplo n.º 5
0
 def obj_to_bytes(self, obj):
     try:
         obj_str = json.dumps(obj)
         bytes = ' '.join(format(ord(letter), 'b') for letter in obj_str)
         # bytes = ' '.join(format(ord(letter), 'b') for letter in obj)
         return bytes
     except Exception as ex:
         raise CacheException(ErrorCodes.GENERAL_ERROR, str(ex), None)
Exemplo n.º 6
0
 def invalidate_cache_by_prefix(self, prefix):
     try:
         if settings.CACHE_ENABLED:
             for key in self.get_redis_instance().scan_iter(prefix + ":*"):
                 self.invalidate_cache(key)
         else:
             pass
     except Exception as ex:
         raise CacheException(ErrorCodes.GENERAL_ERROR, str(ex), None)
Exemplo n.º 7
0
 def put_object_in_cache(self, key, object):
     try:
         if settings.CACHE_ENABLED:
             pickled_object = pickle.dumps(object)
             return self.get_redis_instance().set(key, pickled_object)
         else:
             return None
     except Exception as ex:
         raise CacheException(ErrorCodes.GENERAL_ERROR, str(ex), None)
Exemplo n.º 8
0
 def put_in_cache(self, key, value):
     try:
         if settings.CACHE_ENABLED:
             return self.get_redis_instance().set(key,
                                                  self.obj_to_bytes(value))
         else:
             return None
     except Exception as ex:
         raise CacheException(ErrorCodes.GENERAL_ERROR, str(ex), None)
Exemplo n.º 9
0
 def connect(self):
     try:
         if self.connection is None:
             self.connection = redis.Redis(host=settings.CACHE_HOST,
                                           port=settings.CACHE_PORT,
                                           password=settings.CACHE_PASS,
                                           db=0)
             # self.connection = get_redis_connection('default')
     except Exception as ex:
         raise CacheException(ErrorCodes.GENERAL_ERROR, str(ex), None)
Exemplo n.º 10
0
 def scan(self, prefix=""):
     try:
         if settings.CACHE_ENABLED:
             list_keys, val = self.get_redis_instance().scan(
                 0, prefix + "*", None)
             return list_keys
         else:
             return list()
     except Exception as ex:
         raise CacheException(ErrorCodes.GENERAL_ERROR, str(ex), None)
Exemplo n.º 11
0
    def get_cached_objects_by_keys(self, list_keys):
        try:
            if settings.CACHE_ENABLED:
                cached_objects = self.get_redis_instance().mget(list_keys)
                cached_objects = [pickle.loads(i) for i in cached_objects if i]

                return cached_objects
            else:
                return list()
        except Exception as ex:
            raise CacheException(ErrorCodes.GENERAL_ERROR, str(ex), None)
Exemplo n.º 12
0
 def has_key(self, key):
     try:
         if settings.CACHE_ENABLED:
             if self.get_redis_instance().exists(key):
                 return True
             else:
                 return False
         else:
             return False
     except Exception as ex:
         raise CacheException(ErrorCodes.GENERAL_ERROR, str(ex), None)
Exemplo n.º 13
0
 def get_cached(self, key):
     try:
         if settings.CACHE_ENABLED:
             if self.has_key(key):
                 return self.bytes_to_obj(
                     self.get_redis_instance().get(key))
             else:
                 return None
         else:
             return None
     except Exception as ex:
         raise CacheException(ErrorCodes.GENERAL_ERROR, str(ex), None)
Exemplo n.º 14
0
    def get_object_from_cache(self, key):
        try:
            # if self.has_key(key):
            #     return pickle.loads(self.get_redis_instance().get(key))
            # else:
            #     return None
            if settings.CACHE_ENABLED:
                data_object = self.get_redis_instance().get(key)
                if data_object is not None:
                    data_object = pickle.loads(data_object)

                return data_object
            else:
                return None
        except Exception as ex:
            raise CacheException(ErrorCodes.GENERAL_ERROR, str(ex), None)
Exemplo n.º 15
0
    def get_cached_by_ids(self, prefix, keys):
        cached_objects = []
        non_cached_ids = []
        try:
            if settings.CACHE_ENABLED:
                for key in keys:
                    cache_key = prefix + ":" + str(key)
                    if self.has_key(cache_key):
                        cached_objects.append(self.get_cached(cache_key))
                    else:
                        non_cached_ids.append(key)

                return cached_objects, non_cached_ids
            else:
                return cached_objects, non_cached_ids
        except Exception as ex:
            raise CacheException(ErrorCodes.GENERAL_ERROR, str(ex), None)
Exemplo n.º 16
0
    def get_cached_objects_by_ids(self, prefix, keys):
        cached_objects = []
        non_cached_ids = []
        try:
            if settings.CACHE_ENABLED:
                list_with_prefix = [prefix + ":" + s for s in keys]

                cached_objects = self.get_redis_instance().mget(
                    list_with_prefix)
                cached_objects = [pickle.loads(i) for i in cached_objects if i]

                # exists_keys = [str(each_object.get_id()) for each_object in cached_objects]
                # non_cached_ids = [x for x in keys if x not in exists_keys]

                return cached_objects
            else:
                return cached_objects
        except Exception as ex:
            raise CacheException(ErrorCodes.GENERAL_ERROR, str(ex), None)