config.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. """
  2. encoding: UTF-8
  3. @author:clx
  4. @file:config.py.py
  5. @time:2023/5/31 12:48
  6. """
  7. import json
  8. from minio import Minio
  9. from cacheout import Cache
  10. # 从配置文件读取设置
  11. class MinioOperate:
  12. def __init__(self):
  13. with open(r"D:\pythonProject\django\fastapi_01\config\config.json", "r") as f:
  14. self.__config = json.load(f)
  15. self.minio_client = None
  16. def link_minio(self):
  17. self.minio_client = Minio(**self.__config["minio"])
  18. return self.minio_client
  19. def create_bucket(self,buckets:[]):
  20. for bucket_name in buckets:
  21. if not self.minio_client.bucket_exists(bucket_name):
  22. try:
  23. self.minio_client.make_bucket(bucket_name)
  24. except Exception as e:
  25. print(f"Bucket creation failed: {e}")
  26. else:
  27. print(f"Bucket {bucket_name} already exists")
  28. class SetCache:
  29. def __init__(self,maxsize,ttl):
  30. self.cache = Cache(maxsize=maxsize, ttl=ttl)
  31. def get(self,uid):
  32. self.data = self.cache.get(uid)
  33. return self.data
  34. def add(self,uid,data):
  35. self.cache.add(uid,data)