File tree Expand file tree Collapse file tree 2 files changed +53
-45
lines changed
Expand file tree Collapse file tree 2 files changed +53
-45
lines changed Original file line number Diff line number Diff line change 1+ """app.caches.py"""
2+ # from walrus import Database
3+ import logging
4+ import asyncio
5+ import functools
6+
7+ import aiocache
8+
9+ import config
10+
11+ LOGGER = logging .getLogger (name = "app.caches" )
12+
13+ SETTINGS = config .get_settings ()
14+
15+ if SETTINGS .rediscloud_url :
16+ REDIS_URL = SETTINGS .rediscloud_url
17+ else :
18+ REDIS_URL = SETTINGS .local_redis_url
19+
20+
21+ @functools .lru_cache ()
22+ def get_cache (namespace , redis = False ) -> aiocache .RedisCache :
23+ """Retunr """
24+ if redis :
25+ return aiocache .RedisCache (
26+ endpoint = REDIS_URL .host ,
27+ port = REDIS_URL .port ,
28+ password = REDIS_URL .password ,
29+ namespace = namespace ,
30+ create_connection_timeout = 5 ,
31+ )
32+ return aiocache .SimpleMemoryCache (namespace = namespace )
33+
34+
35+ CACHE = get_cache ("test" , redis = False )
36+
37+
38+ async def cach_test ():
39+ try :
40+ await CACHE .set ("foo" , {"foobar" : "bar" }, ttl = 30 )
41+ except OSError as redis_err :
42+ LOGGER .error (f"Redis Error: { redis_err } " )
43+ return
44+ print (await CACHE .get ("foo" ))
45+ await CACHE .close ()
46+
47+
48+ if __name__ == "__main__" :
49+ # print(REDIS_DB)
50+ # h = REDIS_DB.Hash("Test Hash")
51+ # h["foo"] = "bar"
52+ # print(h)
53+ asyncio .get_event_loop ().run_until_complete (cach_test ())
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments