mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-08-06 21:20:22 +03:00
Refactoring code
This commit is contained in:
parent
42b0c37169
commit
af9c904fb9
|
@ -23,18 +23,18 @@ __log__ = logging.getLogger(__name__)
|
||||||
|
|
||||||
class RedisSession(MemorySession):
|
class RedisSession(MemorySession):
|
||||||
|
|
||||||
log = None
|
|
||||||
session_name = None
|
session_name = None
|
||||||
redis_connection = None
|
redis_connection = None
|
||||||
hive_prefix = None
|
hive_prefix = None
|
||||||
sess_prefix = None
|
sess_prefix = None
|
||||||
pack_func = None
|
pack_func = None
|
||||||
unpack_func = None
|
unpack_func = None
|
||||||
|
use_indents = True
|
||||||
|
|
||||||
def __init__(self, session_name=None, redis_connection=None, hive_prefix=None,
|
def __init__(self, session_name=None, redis_connection=None, hive_prefix=None,
|
||||||
pack_func=PACK_FUNC, unpack_func=UNPACK_FUNC):
|
pack_func=PACK_FUNC, unpack_func=UNPACK_FUNC):
|
||||||
if not isinstance(session_name, (str, bytes)):
|
if not isinstance(session_name, (str, bytes)):
|
||||||
raise TypeError("Session name must be a string or bytes")
|
raise TypeError("Session name must be a string or bytes.")
|
||||||
|
|
||||||
if not redis_connection or not isinstance(redis_connection, redis.StrictRedis):
|
if not redis_connection or not isinstance(redis_connection, redis.StrictRedis):
|
||||||
raise TypeError('The given redis_connection must be a Redis instance.')
|
raise TypeError('The given redis_connection must be a Redis instance.')
|
||||||
|
@ -52,17 +52,18 @@ class RedisSession(MemorySession):
|
||||||
|
|
||||||
self.save_entities = True
|
self.save_entities = True
|
||||||
|
|
||||||
self.feed_sessions()
|
self.feed_session()
|
||||||
|
|
||||||
def _pack(self, o, **kwargs):
|
def _pack(self, o, **kwargs):
|
||||||
if self.pack_func == "json":
|
if self.pack_func == "json":
|
||||||
kwargs["indent"] = 2
|
if self.use_indents:
|
||||||
|
kwargs["indent"] = 2
|
||||||
return json.dumps(o, **kwargs) if self.pack_func == "json" else pickle.dumps(o, **kwargs)
|
return json.dumps(o, **kwargs) if self.pack_func == "json" else pickle.dumps(o, **kwargs)
|
||||||
|
|
||||||
def _unpack(self, o, **kwargs):
|
def _unpack(self, o, **kwargs):
|
||||||
return json.loads(o, **kwargs) if self.unpack_func == "json" else pickle.loads(o, **kwargs)
|
return json.loads(o, **kwargs) if self.unpack_func == "json" else pickle.loads(o, **kwargs)
|
||||||
|
|
||||||
def feed_sessions(self):
|
def feed_session(self):
|
||||||
try:
|
try:
|
||||||
s = self._get_sessions()
|
s = self._get_sessions()
|
||||||
if len(s) == 0:
|
if len(s) == 0:
|
||||||
|
@ -289,4 +290,3 @@ class RedisSession(MemorySession):
|
||||||
self.redis_connection.set(key, self._pack(s))
|
self.redis_connection.set(key, self._pack(s))
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
__log__.exception(ex.args)
|
__log__.exception(ex.args)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user