Skip to content

Commit 609345b

Browse files
committed
use memory cache
1 parent 2bed4b3 commit 609345b

File tree

2 files changed

+53
-45
lines changed

2 files changed

+53
-45
lines changed

app/caches.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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())

app/redis_cache.py

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)