Skip to main content

一个快速使用异步redis优化数据库查询项目

Project description

easycache

本项目提供一个基于异步aioredis作为缓存,达到:

  1. 通过异步redis查询,优化数据库查询。
  2. 通过装饰器快速使用,并且不用在业务代码中修改逻辑。
  3. 基于aioredis2.0+创建redis connections pool,全局只需维护一个redis connections pool,节省维护连接池的资源。

image-easycache

在数据库和用户之间增加redis层,通过装饰器实现达到快速使用,不影响原来业务代码逻辑的目的。

说明

1. EasyCache : class

创建easycache装饰器对象,将参数redis设置为默认redis,redis应该是一个redis client。

2. easycache.find

参数:

名称 类型 说明
prefix str redis key的前缀, 必填
redis aioredis.Redis redis client,为空时使用默认redis
key_expire_time int key expire time, 默认为10min
key_param_index int 数据库主键在参数列表中的位置,第一个位置为1
key_param_name str 数据库主键参数的名称,传参时必须使用命名参数
opt_param2key_func function 将参数转为redis key的函数,为空时默认参数为str或者ObjectId类型

逻辑:

如果获取参数正确且缓存命中,则直接返回缓存的结果,否则调用func,并在获取参数正确的情况下将结果存入缓存。

3. easycache.delete

参数:

名称 类型 说明
prefix str redis key的前缀, 必填
redis aioredis.Redis redis client,为空时使用默认redis
key_expire_time int key expire time, 默认为10min
key_param_index int 数据库主键在参数列表中的位置,第一个位置为1
key_param_name str 数据库主键参数的名称,传参时必须使用命名参数
opt_param2keys_func function 将参数转为redis key list的函数,为空时默认参数为str或者ObjectId类型

逻辑:

如果获取参数正确则删除对应key。

example

1. 维护一个redis connections pool

import aioredis

redis_pool = aioredis.ConnectionPool.from_url(
    f'redis://localhost:6379', decode_responses=True,
)
redis_client = aioredis.Redis(connection_pool=redis_pool)

这里推荐同时调用aioredis.Redis定义一个redis client,并且之后使用redis client代替redis pool。

2. 定义一个easycache装饰器

from init_redis_pool import redis_client
import easycache

easycache = easycache.EasyCache(redis_client)

3.使用装饰器

如需优化以id为条件的数据库查询,且id是唯一的,则在查询的接口调用find,在更新以及删除的接口调用delete。prefinx可以是“数据库名:数据库表名”, key_param_name是findtest函数的参数列表中,传入id的参数名。

@easycache.find(prefix='testcache', key_param_name='id')
async def findtest(*, id):
    await asyncio.sleep(1)
    now = datetime.datetime.now()
    dir = {'time':now}
    return dir
	
loop = asyncio.get_event_loop()
future = asyncio.create_task(findtest(id='921h3918chachs'))
loop.run_until_complete()

在更新以及删除的接口调用delete

@easycache.delete(prefix='testcache', key_param_name='id')
async def deletetest(*, id, other):
    print('delete test ')
    await asyncio.sleep(1)
    return other
	
loop = asyncio.get_event_loop()
future = asyncio.create_task(deletetest(id='921h3918chachs' other='test delete'))
loop.run_until_complete()

在批量更新以及删除的接口调用delete,并且定义处理批量参数的function

def delete2_param2keys(ids):
    res = []
    ids_l = ids['list']
    for id in ids_l:
        res.append('testcache:' + id['key'])
    return res

@easycache.delete(key_param_name='ids', opt_param2keys_func=delete2_param2keys)
async def deletetest2(*, ids:dict, other):
    print('delete test two running')
    await asyncio.sleep(3)
    return other

mp = {'list' : [{'key' : '921h3918chachs'}, {'key' : '9989898989891sa'}]}
loop = asyncio.get_event_loop()
future = asyncio.create_task(deletetest2(id=mp other='test delete'))
loop.run_until_complete()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rosetta-easycache-0.0.6.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rosetta_easycache-0.0.6-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file rosetta-easycache-0.0.6.tar.gz.

File metadata

  • Download URL: rosetta-easycache-0.0.6.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.26.0 requests-toolbelt/0.9.1 urllib3/1.26.6 tqdm/4.60.0 importlib-metadata/4.6.3 keyring/18.0.1 rfc3986/2.0.0 colorama/0.4.3 CPython/3.8.10

File hashes

Hashes for rosetta-easycache-0.0.6.tar.gz
Algorithm Hash digest
SHA256 e95a1fdd92a0da75b05a759c3108734406c071c0f1caff58d99b01cdae14c704
MD5 8bb154f3ec14fb571ff167dae35fb553
BLAKE2b-256 030cd6e36c351c0f721b29c48feba90cb1d9a1fa8a3e6ac2eb79dd50b2bbb591

See more details on using hashes here.

File details

Details for the file rosetta_easycache-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: rosetta_easycache-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.26.0 requests-toolbelt/0.9.1 urllib3/1.26.6 tqdm/4.60.0 importlib-metadata/4.6.3 keyring/18.0.1 rfc3986/2.0.0 colorama/0.4.3 CPython/3.8.10

File hashes

Hashes for rosetta_easycache-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 051d68ddf23ef7176c60acd2283119c9407051f695aa00b56223d838f29f2b0b
MD5 61dcd2da218d1caae71fc260a7ce17df
BLAKE2b-256 f8d8a419db0efb19dbcf463956cf9f4bcf5c66d3f2f629dc68f97a5e28033e5e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page