Aestate
Cache.py
Go to the documentation of this file.
1 # -*- utf-8 -*-
2 import os
3 import threading
4 from collections import OrderedDict
5 from typing import List
6 
7 from aestate.exception import LogStatus
8 from aestate.util import others
9 from aestate.util.others import write
10 from aestate.work.Modes import Singleton
11 
12 
14  """缓存对象"""
15 
16  def __init__(self, key, value):
17  super(CacheItem, self).__init__()
18 
19 
20 class CacheManage(List):
21  """
22  缓存管理
23 
24  1.当内存满足系统运行内存的1/10时,满足最大限度数据内容,保证数据完整性的同时保留数据
25 
26  2.当单次查询数据大于阈值时,保留数据并不在扩大缓存空间,数据完整保留,但不再清理,直到处于第二缓存空间更多查询数据量再次大于阈值时清理
27 
28  3.当通过aestate改变数据时记录数据变更信息,并重新将数据写入缓存,移除旧缓存数据,这将意味着非通过aestate修改的数据不可被检测到
29 
30  4.扩容策略:当前内存>=当前容量1/2时,重新计算查询数据量
31 
32  5.流量计算方式:当前缓存大小 + (当前缓存大小 / 上次扩容时间至当前时间段内插入的新内容数量) * 2 * 当前缓存大小
33 
34  6.移除方案:时间段内缓存查询次数最少内存最大优先,当 (A次数-B次数) * 10 <= (A占用内存-B占用内存),优先删除B
35  """
36 
37  def __init__(self):
38  # 初始内存大小为256byte
39  super(CacheManage).__init__([])
40  self.capacity = 256
41 
42  def __new__(cls, *args, **kwargs):
43  """
44  单例管理缓存内容
45  """
46  instance = Singleton.createObject(cls)
47  return instance
48 
49  def clean(self):
50  pass
51 
52 
53 class PojoContainer:
54  def __init__(self):
55  self.solvent = []
56 
57  def __add__(self, __object):
58  self.solvent.append(__object)
59 
60  @property
61  def size(self) -> int:
62  return len(self.solvent)
63 
64  def get(self, name):
65  for item in self.solvent:
66  if item._type == name:
67  return item._object
68  return None
69 
70 
72  def __init__(self, _type, _object):
73  super(PojoItemCache).__init__()
74  self._type = _type
75  self._object = _object
76 
77 
78 class PojoManage:
79  """管理pojo的缓存"""
80  _instance_lock = threading.RLock()
81  pojo_list = PojoContainer()
82 
83  def append(self, _cls_name: type, _object):
84  self.pojo_list + PojoItemCache(_type=_cls_name, _object=_object)
85 
86  @staticmethod
87  def get(_cls, *args, **kwargs):
88  this = PojoManage()
89  _class_object_ = object.__new__(_cls)
90  cls_name = others.fullname(_class_object_)
91  _obj = this.pojo_list.get(cls_name)
92  if _obj is None:
93  this.append(cls_name, _class_object_)
94  _obj = this.pojo_list.get(cls_name)
95  [setattr(_obj, k, v) for k, v in kwargs.items()]
96  return _obj
97  # return _obj
98 
99  def __new__(cls, *args, **kwargs):
100  instance = Singleton.createObject(cls)
101  return instance
102 
103 
104 class LogCache:
105  _instance_lock = threading.RLock()
106  # 是否已经显示logo了
107  info_logo_show = False
108  warn_logo_show = False
109  error_logo_show = False
110  # 文件名,当满足最大时将会使用一个新的文件作为储存日志
111  info_file_name = []
112  warn_file_name = []
113  error_file_name = []
114 
115  def get_filename(self, path, max_clear, status):
116 
117  if status == LogStatus.Info:
118  center_name = 'info'
119  oa = self.info_file_name
120  logo_show = 'info_logo_show'
121  elif status == LogStatus.Error:
122  center_name = 'error'
123  oa = self.warn_file_name
124  logo_show = 'error_logo_show'
125  elif status == LogStatus.Warn:
126  center_name = 'warn'
127  oa = self.error_file_name
128  logo_show = 'warn_logo_show'
129  else:
130  center_name = 'info'
131  oa = self.info_file_name
132  logo_show = 'info_logo_show'
133 
134  _path = os.path.join(path, center_name)
135  if len(oa) == 0:
136  oa.append(others.date_format(fmt='%Y.%m.%d.%H.%M.%S') + '.log')
137  setattr(self, logo_show, False)
138  else:
139  if not os.path.exists(os.path.join(_path, oa[len(oa) - 1])):
140  write(os.path.join(_path, '.temp'), '')
141  setattr(self, logo_show, False)
142  if os.path.getsize(os.path.join(_path, oa[len(oa) - 1])) >= max_clear:
143  oa.append(others.date_format(fmt='%Y.%m.%d.%H.%M.%S') + '.log')
144  setattr(self, logo_show, False)
145 
146  return os.path.join(center_name, oa[len(oa) - 1])
147 
148  def __new__(cls, *args, **kwargs):
149  instance = Singleton.createObject(cls)
150  return instance
aestate.work.Cache.PojoContainer
Definition: Cache.py:53
aestate.work.Cache.LogCache
Definition: Cache.py:104
aestate.work.Cache.LogCache.warn_file_name
list warn_file_name
Definition: Cache.py:112
aestate.work.Cache.LogCache.info_file_name
list info_file_name
Definition: Cache.py:111
aestate.work.Cache.CacheItem
Definition: Cache.py:13
aestate.work.Cache.PojoContainer.solvent
solvent
Definition: Cache.py:55
aestate.work.Cache.PojoManage
Definition: Cache.py:78
aestate.work.Cache.PojoContainer.size
int size(self)
Definition: Cache.py:61
aestate.util.others
Definition: others.py:1
aestate.work.Cache.CacheItem.__init__
def __init__(self, key, value)
Definition: Cache.py:16
aestate.work.Cache.CacheManage
Definition: Cache.py:20
aestate.work.Cache.PojoManage.append
def append(self, type _cls_name, _object)
Definition: Cache.py:83
aestate.work.Cache.LogCache.error_file_name
list error_file_name
Definition: Cache.py:113
aestate.work.Cache.LogCache.get_filename
def get_filename(self, path, max_clear, status)
Definition: Cache.py:115
aestate.work.Cache.CacheManage.clean
def clean(self)
Definition: Cache.py:49
aestate.ajson.sim.OrderedDict
def OrderedDict
Definition: __init__.py:26
aestate.work.Cache.PojoItemCache._object
_object
Definition: Cache.py:75
aestate.work.Cache.PojoManage.__new__
def __new__(cls, *args, **kwargs)
Definition: Cache.py:99
aestate.util
Definition: __init__.py:1
aestate.work.Cache.CacheManage.__new__
def __new__(cls, *args, **kwargs)
Definition: Cache.py:42
aestate.work.Modes
Definition: Modes.py:1
aestate.exception
Definition: __init__.py:1
aestate.work.Cache.PojoManage.get
def get(_cls, *args, **kwargs)
Definition: Cache.py:87
aestate.work.Cache.PojoContainer.get
def get(self, name)
Definition: Cache.py:64
aestate.work.Cache.PojoItemCache
Definition: Cache.py:71
aestate.work.Cache.PojoContainer.__init__
def __init__(self)
Definition: Cache.py:54
aestate.work.Cache.PojoManage.pojo_list
pojo_list
Definition: Cache.py:81
aestate.work.Cache.LogCache.__new__
def __new__(cls, *args, **kwargs)
Definition: Cache.py:148
aestate.work.Cache.PojoContainer.__add__
def __add__(self, __object)
Definition: Cache.py:57
aestate.work.Cache.CacheManage.__init__
def __init__(self)
Definition: Cache.py:37
aestate.work.Cache.CacheManage.capacity
capacity
Definition: Cache.py:40
aestate.util.others.write
def write(path, *content)
Definition: others.py:82
aestate.work.Cache.PojoItemCache._type
_type
Definition: Cache.py:74
aestate.work.Cache.PojoItemCache.__init__
def __init__(self, _type, _object)
Definition: Cache.py:72