| | |
| | | import logging |
| | | import queue |
| | | import time |
| | | from threading import Thread |
| | | |
| | | import redis |
| | | |
| | | import constant |
| | | from log_module.log import logger_redis_debug |
| | | from utils import tool |
| | | |
| | | config = constant.REDIS_CONFIG |
| | | |
| | |
| | | |
| | | |
| | | class RedisUtils: |
| | | __async_task_queue = queue.Queue() |
| | | __async_task_queue = queue.Queue(maxsize=1024) |
| | | |
| | | @classmethod |
| | | def exec(cls, method_name, key, lamada_method): |
| | |
| | | try: |
| | | return lamada_method() |
| | | finally: |
| | | logger_redis_debug.info("{}({}):{}", method_name, round((time.time() - __start_time) * 1000, 3), key) |
| | | # logger_redis_debug.info("{}({}):{}", method_name, round((time.time() - __start_time) * 1000, 3), key) |
| | | pass |
| | | |
| | | @classmethod |
| | | def get(cls, redis_, key, auto_free=True): |
| | |
| | | |
| | | @classmethod |
| | | def delete_async(cls, db, key, auto_free=True): |
| | | __start_time = time.time() |
| | | cls.add_async_task(db, "delete", (key)) |
| | | logger_redis_debug.info("delete_async({}):{}", round((time.time() - __start_time) * 1000, 3), key) |
| | | logger_redis_debug.info("delete_async({}):{}", 0, key) |
| | | |
| | | @classmethod |
| | | def keys(cls, redis_, key, auto_free=True): |
| | |
| | | |
| | | @classmethod |
| | | def setex_async(cls, db, key, expire, val, auto_free=True): |
| | | __start_time = time.time() |
| | | cls.add_async_task(db, "setex", (key, expire, val)) |
| | | logger_redis_debug.info("setex_async({}):{}", round((time.time() - __start_time) * 1000, 3), key) |
| | | logger_redis_debug.info("setex_async({}):{}", 0, key) |
| | | |
| | | @classmethod |
| | | def setnx(cls, redis_, key, val, auto_free=True): |
| | |
| | | return cls.exec("expire", key, lambda: redis_.expire(key, expire)) |
| | | |
| | | @classmethod |
| | | def expire_async(cls, db, key, expire, auto_free=True): |
| | | cls.add_async_task(db, "expire", (key, expire)) |
| | | logger_redis_debug.info("expire_async({}):{}", 0, key) |
| | | |
| | | @classmethod |
| | | def sadd(cls, redis_, key, val, auto_free=True): |
| | | return cls.exec("sadd", key, lambda: redis_.sadd(key, val)) |
| | | |
| | | @classmethod |
| | | def sadd_async(cls, db, key, val, auto_free=True): |
| | | cls.add_async_task(db, "sadd", (key, val)) |
| | | logger_redis_debug.info("sadd_async({}):{}", 0, key) |
| | | |
| | | @classmethod |
| | | def sismember(cls, redis_, key, val, auto_free=True): |
| | |
| | | return cls.exec("srem", key, lambda: redis_.srem(key, val)) |
| | | |
| | | @classmethod |
| | | def srem_async(cls, db, key, val, auto_free=True): |
| | | cls.add_async_task(db, "srem", (key, val)) |
| | | logger_redis_debug.info("srem_async({}):{}", 0, key) |
| | | |
| | | @classmethod |
| | | def incrby(cls, redis_, key, num, auto_free=True, _async=False): |
| | | return cls.exec("incrby", key, lambda: redis_.incrby(key, num)) |
| | | |
| | | @classmethod |
| | | def incrby_async(cls, db, key, num, auto_free=True): |
| | | __start_time = time.time() |
| | | cls.add_async_task(db, "incrby", (key, num)) |
| | | logger_redis_debug.info("incrby_async({}):{}", round((time.time() - __start_time) * 1000, 3), key) |
| | | logger_redis_debug.info("incrby_async({}):{}", 0, key) |
| | | |
| | | @classmethod |
| | | def lpush(cls, redis_, key, val, auto_free=True): |
| | |
| | | if data: |
| | | db = data[0] |
| | | method_name = data[1] |
| | | print(db,method_name) |
| | | args = data[2] |
| | | _redis = RedisManager(db).getRedisNoPool() |
| | | method = getattr(_redis, method_name) |
| | | if type(args) == tuple: |
| | | result = method(*args) |
| | | print(result) |
| | | else: |
| | | result = method(args) |
| | | print(result) |
| | | |
| | | except Exception as e1: |
| | | logging.exception(e1) |
| | | pass |
| | | time.sleep(1) |
| | | |
| | | |
| | | if __name__ == "__main__": |
| | | RedisUtils.setex_async(0, "test", tool.get_expire(), "123123") |
| | | print("大小",RedisUtils.get_async_task_count()) |
| | | RedisUtils.incrby_async(0, "test_1", 1) |
| | | print("大小", RedisUtils.get_async_task_count()) |
| | | RedisUtils.delete_async(0, "test") |
| | | print("大小", RedisUtils.get_async_task_count()) |
| | | RedisUtils.run_loop() |
| | | redis = RedisManager(1).getRedis() |
| | | db = redis.connection_pool.connection_kwargs['db'] |
| | | print(db) |