Skip to content

Commit

Permalink
implement clean for food cache
Browse files Browse the repository at this point in the history
  • Loading branch information
mhewedy committed May 24, 2024
1 parent e714bbe commit 81a0530
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
17 changes: 15 additions & 2 deletions cache.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import logging
import os
import pickle
import shutil

import util

root_fs_cache = util.get_root_fs() + '/cache'


def _cache_path(namespace, key):
def _cache_path(namespace, key=None):
cache_namespace = root_fs_cache + '/' + namespace
os.makedirs(cache_namespace, exist_ok=True)
return os.path.join(cache_namespace, f'{key}.pkl')
if key:
return os.path.join(cache_namespace, f'{key}.pkl')
else:
return cache_namespace


def get(namespace, key):
Expand All @@ -26,3 +30,12 @@ def get(namespace, key):
def put(namespace, key, value):
with open(_cache_path(namespace, f'{key.strip().lower()}'), 'wb') as f:
pickle.dump(value, f)


def clean(namespace):
cache_namespace = _cache_path(namespace)
if os.path.exists(cache_namespace):
shutil.rmtree(cache_namespace)
logging.info(f'Cache for namespace {namespace} has been cleaned.')
else:
logging.warning(f'Cache for namespace {namespace} does not exist.')
8 changes: 6 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from telegram.constants import ParseMode
from telegram.ext import ContextTypes

import cache
import food
import util
from order import FileSystemOrderManager
Expand Down Expand Up @@ -86,9 +87,12 @@ async def clear_command(update: Update, context: ContextTypes.DEFAULT_TYPE):

order_manager.clear_orders()
msg = "تم مسح جميع الطلبات بنجاح"
if "+selection_history" in context.args:
msg += " و تم مسح جميع اختيارات المستخدمين أيضا"
if "+selection" in context.args:
msg += " و تم مسح جميع اختيارات المستخدمين"
userSelector.clear_history()
if "+food" in context.args:
msg += " و تم مسح مخزون الأكلات"
cache.clean("food")
await update.message.reply_text(msg)


Expand Down

0 comments on commit 81a0530

Please sign in to comment.