-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdatabase.py
More file actions
47 lines (27 loc) · 1.07 KB
/
database.py
File metadata and controls
47 lines (27 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from pymongo import MongoClient
from dotenv import load_dotenv
from datetime import timedelta, datetime
import os, pytz
load_dotenv()
client= MongoClient(os.getenv("URI"))
class CacheManager:
def __init__(self):
self.collection= client["cached_comics"]["comics"]
self.cache_interval= timedelta(hours= 24)
self.time_zone= pytz.UTC
def create_cache(self, data):
cache_time= datetime.now(tz= self.time_zone)
data["cache_time"]= cache_time
self.collection.insert_one(data)
def update_cache(self, filter, updated_data):
cache_time= datetime.now(tz= self.time_zone)
updated_data["$set"]["cache_time"]= cache_time
self.collection.update_one(filter, updated_data)
def fetch_cache(self, filter):
return self.collection.find_one(filter)
def is_cache_expired(self, cache_time):
cache_time_aware= self.time_zone.localize(cache_time)
now= datetime.now(self.time_zone)
if now - cache_time_aware < self.cache_interval:
return False
return True