Skip to content

Commit

Permalink
cache food items
Browse files Browse the repository at this point in the history
  • Loading branch information
mhewedy committed May 23, 2024
1 parent 2825923 commit 9299276
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
18 changes: 18 additions & 0 deletions cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import logging

import diskcache

import util

cache = diskcache.Cache(util.get_root_fs() + '/cache')


def put(namespace, key: str, value):
cache.set(f'{namespace}:{key.strip().lower()}', value)


def get(namespace, key: str):
result = cache.get(f'{namespace}:{key.strip().lower()}')
if result is not None:
logging.info(f'cache hit for {namespace}:{key} -> {result}')
return result
8 changes: 7 additions & 1 deletion food.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import g4f.models
from g4f.client import Client

import cache
from util import retry_function

client = Client()
Expand All @@ -23,8 +24,13 @@ def _is_food(text):


def is_food(text):
result = cache.get('food', text)
if result is not None: return result

try:
return retry_function(_is_food, text=text)
result = retry_function(_is_food, text=text)
cache.put('food', text, result)
return result
except Exception as e:
logging.error(e)
return False
7 changes: 5 additions & 2 deletions order.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import ast
import json
import logging
import os
from abc import ABC
from pathlib import Path

import cache
import util


class OrderManager(ABC):
def __init__(self):
self.orders = {}

def add_order(self, message_id, user, order):
cache.put('food', order, True)
self.orders[(message_id, user)] = order

def delete_order(self, user):
Expand All @@ -31,7 +34,7 @@ class InMemoryOrderManager(OrderManager):


class FileSystemOrderManager(OrderManager):
def __init__(self, file_path=os.getenv('VOLUME_ROOT_FS', '/tmp') + '/orders.json'):
def __init__(self, file_path=util.get_root_fs() + '/orders.json'):
super().__init__()
self.file_path = Path(file_path)
logging.info('loading orders from: {}'.format(self.file_path))
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ charset-normalizer==3.3.2
distro==1.9.0
exceptiongroup==1.2.1
frozenlist==1.4.1
g4f==0.3.1.3
g4f==0.3.1.9
h11==0.14.0
httpcore==1.0.5
httpx==0.27.0
Expand All @@ -38,3 +38,4 @@ typing_extensions==4.11.0
tzlocal==5.2
urllib3==2.2.1
yarl==1.9.4
diskcache==5.6.3
5 changes: 5 additions & 0 deletions util.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import random
import time
from typing import Any
Expand Down Expand Up @@ -41,3 +42,7 @@ def get_congrats_msg():
"اليوم يومك النهارده يا",
"مبروك عليك ... هتطلب لنا اليوم يا"
])


def get_root_fs():
return os.getenv('VOLUME_ROOT_FS', '/tmp')

0 comments on commit 9299276

Please sign in to comment.