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.1.1.tar.gz (5.4 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.1.1-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: rosetta-easycache-0.1.1.tar.gz
  • Upload date:
  • Size: 5.4 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.62.3 importlib-metadata/4.6.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.0

File hashes

Hashes for rosetta-easycache-0.1.1.tar.gz
Algorithm Hash digest
SHA256 77c8f893dbaf83ebe761519c00e61e1f3cd0c8983819b1016530fdc557c0ccd8
MD5 0d6d6566025162c887d530d2a652c817
BLAKE2b-256 255168ad819d099287194a4c0319bb057c2c4e4c625047c5088364ec6caa76e2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: rosetta_easycache-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.0 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.62.3 importlib-metadata/4.6.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.0

File hashes

Hashes for rosetta_easycache-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f252818a84de24ef55cb95f40c6392d5278b740b80b9a343e6c9c2d8706b44f5
MD5 1d76f949adfff627dfa537ecb19e1cd3
BLAKE2b-256 750514c5bca191712ef18524008c4a0a342419855f49ad654e4e5d3001deecf7

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