kuncache 1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- kuncache-1.0/PKG-INFO +13 -0
- kuncache-1.0/kuncache/__init__.py +4 -0
- kuncache-1.0/kuncache/cacheclass.py +338 -0
- kuncache-1.0/kuncache.egg-info/PKG-INFO +13 -0
- kuncache-1.0/kuncache.egg-info/SOURCES.txt +8 -0
- kuncache-1.0/kuncache.egg-info/dependency_links.txt +1 -0
- kuncache-1.0/kuncache.egg-info/requires.txt +1 -0
- kuncache-1.0/kuncache.egg-info/top_level.txt +1 -0
- kuncache-1.0/setup.cfg +4 -0
- kuncache-1.0/setup.py +52 -0
kuncache-1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Metadata-Version: 1.2
|
|
2
|
+
Name: kuncache
|
|
3
|
+
Version: 1.0
|
|
4
|
+
Summary: kuncache
|
|
5
|
+
Home-page: UNKNOWN
|
|
6
|
+
Author: 百里
|
|
7
|
+
Author-email: kwebs@kwebapp.cn
|
|
8
|
+
Maintainer: 坤坤
|
|
9
|
+
Maintainer-email: fk1402936534@qq.com
|
|
10
|
+
License: MIT License
|
|
11
|
+
Description: UNKNOWN
|
|
12
|
+
Keywords: kuncache1.0
|
|
13
|
+
Platform: UNKNOWN
|
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
import os,time,hashlib,copy,threading
|
|
3
|
+
def feagesgvresgaeagis_index(params,index):
|
|
4
|
+
"""判断列表或字典里的索引是否存在
|
|
5
|
+
|
|
6
|
+
params 列表或字典
|
|
7
|
+
|
|
8
|
+
index 索引值
|
|
9
|
+
|
|
10
|
+
return Boolean类型
|
|
11
|
+
"""
|
|
12
|
+
try:
|
|
13
|
+
params[index]
|
|
14
|
+
except KeyError:
|
|
15
|
+
return False
|
|
16
|
+
except IndexError:
|
|
17
|
+
return False
|
|
18
|
+
else:
|
|
19
|
+
return True
|
|
20
|
+
try:
|
|
21
|
+
import kcredis
|
|
22
|
+
from kwebs.config import cache as cacheconfig
|
|
23
|
+
if not feagesgvresgaeagis_index(cacheconfig,'th_lock'):
|
|
24
|
+
cacheconfig['th_lock']=False
|
|
25
|
+
if not feagesgvresgaeagis_index(cacheconfig,'th_lock'):
|
|
26
|
+
cacheconfig['debug']=False
|
|
27
|
+
except:
|
|
28
|
+
cacheconfig={}
|
|
29
|
+
cacheconfig['th_lock']=False
|
|
30
|
+
cacheconfig['debug']=False
|
|
31
|
+
cacheconfig['type']='File' #驱动方式 支持 File Redis Python
|
|
32
|
+
cacheconfig['path']='app/runtime/cachepath' #缓存保存目录
|
|
33
|
+
cacheconfig['expire']=120 #缓存有效期 0表示永久缓存
|
|
34
|
+
cacheconfig['host']='127.0.0.1' #Redis服务器地址
|
|
35
|
+
cacheconfig['port']=6379 #Redis 端口
|
|
36
|
+
cacheconfig['password']='' #Redis登录密码
|
|
37
|
+
cacheconfig['db']=1 #Redis数据库 注:Redis用1或2或3等表示
|
|
38
|
+
cachevalue={}
|
|
39
|
+
def kcwcache_print_log(*strs):
|
|
40
|
+
print(time.strftime("%Y-%m-%d %H:%M:%S"),*strs)
|
|
41
|
+
class vsregrsgtrdhbrhtrsgrshydtrsegregsresgr:
|
|
42
|
+
__name=None
|
|
43
|
+
__values=None
|
|
44
|
+
__config=copy.deepcopy(cacheconfig)
|
|
45
|
+
__redisobj=None
|
|
46
|
+
|
|
47
|
+
__thlock={
|
|
48
|
+
'obj':None,
|
|
49
|
+
'status':False
|
|
50
|
+
}
|
|
51
|
+
def __md5(self,strs):
|
|
52
|
+
"""md5加密"""
|
|
53
|
+
if not strs:
|
|
54
|
+
return strs
|
|
55
|
+
m = hashlib.md5()
|
|
56
|
+
b = strs.encode(encoding='utf-8')
|
|
57
|
+
m.update(b)
|
|
58
|
+
return m.hexdigest()
|
|
59
|
+
def __times(self):
|
|
60
|
+
"""时间戳 精确到秒"""
|
|
61
|
+
return int(time.time())
|
|
62
|
+
def __json_decode(self,jsonstr):
|
|
63
|
+
"""json字符串转python类型"""
|
|
64
|
+
try:
|
|
65
|
+
return eval(jsonstr)
|
|
66
|
+
except Exception:
|
|
67
|
+
return {}
|
|
68
|
+
def __start_th_lock(self):
|
|
69
|
+
"""开启线程锁 多线程中建议开启 注意 这个python多线程锁 而不是数据库事务锁"""
|
|
70
|
+
if not self.__thlock['obj']:
|
|
71
|
+
self.__thlock['obj']=threading.Lock()
|
|
72
|
+
self.__thlock['obj'].acquire()
|
|
73
|
+
self.__thlock['status']=True
|
|
74
|
+
if self.__config['debug']:
|
|
75
|
+
kcwcache_print_log('开启线程锁cache')
|
|
76
|
+
def __close_th_lock(self):
|
|
77
|
+
"""退出线程锁 这个python多线程锁 而不是数据库事务锁"""
|
|
78
|
+
if self.__thlock['status']:
|
|
79
|
+
self.__thlock['obj'].release()
|
|
80
|
+
self.__thlock['status']=False
|
|
81
|
+
if self.__config['debug']:
|
|
82
|
+
kcwcache_print_log('退出线程锁cache')
|
|
83
|
+
def set_config(self,config,th_lock='no'):
|
|
84
|
+
"""设置缓存配置
|
|
85
|
+
|
|
86
|
+
th_lock 是否开启线程锁 多线程中建议开启 注意 这个python多线程锁 而不是redis库事务锁 也可以在配置信息中全局开启
|
|
87
|
+
"""
|
|
88
|
+
if th_lock!='no':
|
|
89
|
+
config['th_lock']=th_lock
|
|
90
|
+
if config['th_lock']:
|
|
91
|
+
self.__start_th_lock()
|
|
92
|
+
self.__config=copy.deepcopy(config)
|
|
93
|
+
return self
|
|
94
|
+
def __setredisobj(self):
|
|
95
|
+
"设置redis链接实例"
|
|
96
|
+
conf=copy.deepcopy(cacheconfig)
|
|
97
|
+
if 'host' in self.__config and self.__config['host']:
|
|
98
|
+
conf['host']=self.__config['host']
|
|
99
|
+
if 'port' in self.__config and self.__config['port']:
|
|
100
|
+
conf['port']=self.__config['port']
|
|
101
|
+
if 'password' in self.__config and self.__config['password']:
|
|
102
|
+
conf['password']=self.__config['password']
|
|
103
|
+
if 'db' in self.__config and self.__config['db']:
|
|
104
|
+
conf['db']=self.__config['db']
|
|
105
|
+
if not self.__redisobj:
|
|
106
|
+
self.__redisobj=kcredis.redis
|
|
107
|
+
self.__redisobj.connect(conf)
|
|
108
|
+
def set_cache(self,name,values,expire = 'no'):
|
|
109
|
+
"""设置缓存
|
|
110
|
+
|
|
111
|
+
参数 name:缓存名
|
|
112
|
+
|
|
113
|
+
参数 values:缓存值
|
|
114
|
+
|
|
115
|
+
参数 expire:缓存有效期 0表示永久 单位 秒
|
|
116
|
+
|
|
117
|
+
return Boolean类型
|
|
118
|
+
"""
|
|
119
|
+
# print(name)
|
|
120
|
+
# exit()
|
|
121
|
+
self.__name=name
|
|
122
|
+
self.__values=values
|
|
123
|
+
if expire != 'no':
|
|
124
|
+
self.__config['expire']=int(expire)
|
|
125
|
+
return self.__seltype('set')
|
|
126
|
+
def get_cache(self,name):
|
|
127
|
+
"""获取缓存
|
|
128
|
+
|
|
129
|
+
return 或者的值
|
|
130
|
+
"""
|
|
131
|
+
self.__name=name
|
|
132
|
+
return self.__seltype('get')
|
|
133
|
+
def del_cache(self,name):
|
|
134
|
+
"""删除缓存
|
|
135
|
+
|
|
136
|
+
return Boolean类型
|
|
137
|
+
"""
|
|
138
|
+
self.__name=name
|
|
139
|
+
return self.__seltype('del')
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def __seltype(self,types):
|
|
144
|
+
"""选择缓存"""
|
|
145
|
+
self.__name=self.__md5(self.__name)
|
|
146
|
+
if self.__config['type'] == 'File':
|
|
147
|
+
if types == 'set':
|
|
148
|
+
res = self.__setfilecache()
|
|
149
|
+
elif types=='get':
|
|
150
|
+
res = self.__getfilecache()
|
|
151
|
+
elif types=='del':
|
|
152
|
+
res = self.__delfilecache()
|
|
153
|
+
elif self.__config['type'] == 'Redis':
|
|
154
|
+
self.__setredisobj()
|
|
155
|
+
if types == 'set':
|
|
156
|
+
res = self.__setrediscache()
|
|
157
|
+
elif types=='get':
|
|
158
|
+
res = self.__getrediscache()
|
|
159
|
+
elif types=='del':
|
|
160
|
+
res = self.__delrediscache()
|
|
161
|
+
# elif self.__config['type'] == 'MySql':
|
|
162
|
+
# self.__setmysqlonj()
|
|
163
|
+
# if types == 'set':
|
|
164
|
+
# res =res self.__setmysqlcache()
|
|
165
|
+
# elif types == 'get':
|
|
166
|
+
# res = self.__getmysqlcache()
|
|
167
|
+
# elif types == 'del':
|
|
168
|
+
# res = self.__delmysqlcache()
|
|
169
|
+
elif self.__config['type'] == 'Python':
|
|
170
|
+
if types == 'set':
|
|
171
|
+
res = self.__setpythoncache()
|
|
172
|
+
elif types == 'get':
|
|
173
|
+
res = self.__getpythoncache()
|
|
174
|
+
elif types == 'del':
|
|
175
|
+
res = self.__delpythoncache()
|
|
176
|
+
else:
|
|
177
|
+
raise Exception("缓存类型错误")
|
|
178
|
+
self.__config=copy.deepcopy(cacheconfig)
|
|
179
|
+
self.__close_th_lock()
|
|
180
|
+
return res
|
|
181
|
+
def __setpythoncache(self):
|
|
182
|
+
"""设置python缓存
|
|
183
|
+
|
|
184
|
+
return Boolean类型
|
|
185
|
+
"""
|
|
186
|
+
data={
|
|
187
|
+
'expire':self.__config['expire'],
|
|
188
|
+
'time':self.__times(),
|
|
189
|
+
'values':self.__values
|
|
190
|
+
}
|
|
191
|
+
cachevalue[self.__name]=data
|
|
192
|
+
return True
|
|
193
|
+
def __getpythoncache(self):
|
|
194
|
+
"""获取python缓存
|
|
195
|
+
|
|
196
|
+
return 缓存的值
|
|
197
|
+
"""
|
|
198
|
+
try:
|
|
199
|
+
ar=cachevalue[self.__name]
|
|
200
|
+
except KeyError:
|
|
201
|
+
return ""
|
|
202
|
+
else:
|
|
203
|
+
if ar['expire'] > 0:
|
|
204
|
+
if (self.__times()-ar['time']) > ar['expire']:
|
|
205
|
+
self.__delpythoncache()
|
|
206
|
+
return ""
|
|
207
|
+
else:
|
|
208
|
+
return ar['values']
|
|
209
|
+
else:
|
|
210
|
+
return ar['values']
|
|
211
|
+
def __delpythoncache(self):
|
|
212
|
+
"""删除python缓存
|
|
213
|
+
|
|
214
|
+
return Boolean类型
|
|
215
|
+
"""
|
|
216
|
+
try:
|
|
217
|
+
del cachevalue[self.__name]
|
|
218
|
+
except KeyError:
|
|
219
|
+
pass
|
|
220
|
+
return True
|
|
221
|
+
# def __setmysqlcache(self): ########################################################################################
|
|
222
|
+
# """设置mysql缓存
|
|
223
|
+
|
|
224
|
+
# return Boolean类型
|
|
225
|
+
# """
|
|
226
|
+
# data=[str(self.__values)]
|
|
227
|
+
# strs="["
|
|
228
|
+
# for k in data:
|
|
229
|
+
# strs=strs+k
|
|
230
|
+
# strs=strs+"]"
|
|
231
|
+
# k=self.__mysqlobj.table('fanshukeji_core_cache').where("name",self.__name).count('id')
|
|
232
|
+
# self.__setmysqlonj()
|
|
233
|
+
# if k:
|
|
234
|
+
# return self.__mysqlobj.table('fanshukeji_core_cache').where("name",self.__name).update({"val":strs,"expire":self.__config['expire'],"time":self.__times()})
|
|
235
|
+
# else:
|
|
236
|
+
# return self.__mysqlobj.table('fanshukeji_core_cache').insert({"name":self.__name,"val":strs,"expire":self.__config['expire'],"time":self.__times()})
|
|
237
|
+
# def __getmysqlcache(self):
|
|
238
|
+
# """获取mysql缓存
|
|
239
|
+
|
|
240
|
+
# return 缓存的值
|
|
241
|
+
# """
|
|
242
|
+
# data=self.__mysqlobj.table('fanshukeji_core_cache').where("name",self.__name).find()
|
|
243
|
+
# if data :
|
|
244
|
+
# if data['expire']>0 and self.__times()-data['time']>data['expire']:
|
|
245
|
+
# self.__setmysqlonj()
|
|
246
|
+
# self.__mysqlobj.table('fanshukeji_core_cache').where("name",self.__name).delete()
|
|
247
|
+
# return False
|
|
248
|
+
# else:
|
|
249
|
+
# return eval(data['val'])[0]
|
|
250
|
+
# else:
|
|
251
|
+
# return False
|
|
252
|
+
# def __delmysqlcache(self):
|
|
253
|
+
# """删除mysql缓存
|
|
254
|
+
|
|
255
|
+
# return Boolean类型
|
|
256
|
+
# """
|
|
257
|
+
# return self.__mysqlobj.table('fanshukeji_core_cache').where("name",self.__name).delete()
|
|
258
|
+
def __setrediscache(self):
|
|
259
|
+
"""设置redis缓存
|
|
260
|
+
|
|
261
|
+
return Boolean类型
|
|
262
|
+
"""
|
|
263
|
+
data=self.__values
|
|
264
|
+
try:
|
|
265
|
+
if self.__config['expire']:
|
|
266
|
+
self.__redisobj.set(self.__name,data,self.__config['expire'])
|
|
267
|
+
else:
|
|
268
|
+
self.__redisobj.set(self.__name,data)
|
|
269
|
+
except:
|
|
270
|
+
return False
|
|
271
|
+
return True
|
|
272
|
+
def __getrediscache(self):
|
|
273
|
+
"""获取redis缓存
|
|
274
|
+
|
|
275
|
+
return 缓存的值
|
|
276
|
+
"""
|
|
277
|
+
lists=self.__redisobj.get(self.__name)
|
|
278
|
+
if lists:
|
|
279
|
+
return lists
|
|
280
|
+
else:
|
|
281
|
+
return False
|
|
282
|
+
def __delrediscache(self):
|
|
283
|
+
"""删除redis缓存
|
|
284
|
+
|
|
285
|
+
return int类型
|
|
286
|
+
"""
|
|
287
|
+
return self.__redisobj.delete(self.__name)
|
|
288
|
+
def __setfilecache(self):
|
|
289
|
+
"""设置文件缓存
|
|
290
|
+
|
|
291
|
+
return Boolean类型
|
|
292
|
+
"""
|
|
293
|
+
data={
|
|
294
|
+
'expire':self.__config['expire'],
|
|
295
|
+
'time':self.__times(),
|
|
296
|
+
'values':self.__values
|
|
297
|
+
}
|
|
298
|
+
if not os.path.exists(self.__config['path']):
|
|
299
|
+
os.makedirs(self.__config['path']) #多层创建目录
|
|
300
|
+
f=open(self.__config['path']+"/"+self.__name,"w")
|
|
301
|
+
f.write(str(data))
|
|
302
|
+
f.close()
|
|
303
|
+
return True
|
|
304
|
+
def __getfilecache(self):
|
|
305
|
+
"""获取文件缓存
|
|
306
|
+
|
|
307
|
+
return 缓存的值
|
|
308
|
+
"""
|
|
309
|
+
try:
|
|
310
|
+
f=open(self.__config['path']+"/"+self.__name,"r")
|
|
311
|
+
except Exception:
|
|
312
|
+
return ""
|
|
313
|
+
json_str=f.read()
|
|
314
|
+
f.close()
|
|
315
|
+
ar=self.__json_decode(json_str)
|
|
316
|
+
|
|
317
|
+
if ar and feagesgvresgaeagis_index(ar,'expire') and ar['expire'] > 0:
|
|
318
|
+
if (self.__times()-ar['time']) > ar['expire']:
|
|
319
|
+
self.__delfilecache()
|
|
320
|
+
return ""
|
|
321
|
+
else:
|
|
322
|
+
return ar['values']
|
|
323
|
+
elif ar and feagesgvresgaeagis_index(ar,'values'):
|
|
324
|
+
return ar['values']
|
|
325
|
+
else:
|
|
326
|
+
return ''
|
|
327
|
+
def __delfilecache(self):
|
|
328
|
+
"""删除文件缓存
|
|
329
|
+
|
|
330
|
+
return Boolean类型
|
|
331
|
+
"""
|
|
332
|
+
if not os.path.exists(self.__config['path']+"/"+self.__name):
|
|
333
|
+
return True
|
|
334
|
+
try:
|
|
335
|
+
os.remove(self.__config['path']+"/"+self.__name)
|
|
336
|
+
except:
|
|
337
|
+
return False
|
|
338
|
+
return True
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Metadata-Version: 1.2
|
|
2
|
+
Name: kuncache
|
|
3
|
+
Version: 1.0
|
|
4
|
+
Summary: kuncache
|
|
5
|
+
Home-page: UNKNOWN
|
|
6
|
+
Author: 百里
|
|
7
|
+
Author-email: kwebs@kwebapp.cn
|
|
8
|
+
Maintainer: 坤坤
|
|
9
|
+
Maintainer-email: fk1402936534@qq.com
|
|
10
|
+
License: MIT License
|
|
11
|
+
Description: UNKNOWN
|
|
12
|
+
Keywords: kuncache1.0
|
|
13
|
+
Platform: UNKNOWN
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
kcredis>=1
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
kuncache
|
kuncache-1.0/setup.cfg
ADDED
kuncache-1.0/setup.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
|
|
2
|
+
# 打包上传 python setup.py sdist upload
|
|
3
|
+
# 安装 python setup.py install
|
|
4
|
+
import os,sys
|
|
5
|
+
from setuptools import setup
|
|
6
|
+
from kuncache import __version__
|
|
7
|
+
confkcws={}
|
|
8
|
+
confkcws['name']='kuncache'
|
|
9
|
+
confkcws['version']=__version__
|
|
10
|
+
confkcws['description']='kuncache'
|
|
11
|
+
confkcws['long_description']=''
|
|
12
|
+
confkcws['license']='MIT License'
|
|
13
|
+
confkcws['url']=''
|
|
14
|
+
confkcws['author']='百里'
|
|
15
|
+
confkcws['author_email']='kwebs@kwebapp.cn'
|
|
16
|
+
confkcws['maintainer']='坤坤'
|
|
17
|
+
confkcws['maintainer_email']='fk1402936534@qq.com'
|
|
18
|
+
def get_file(folder='./',lists=[]):
|
|
19
|
+
lis=os.listdir(folder)
|
|
20
|
+
for files in lis:
|
|
21
|
+
if not os.path.isfile(folder+"/"+files):
|
|
22
|
+
if files=='__pycache__' or files=='.git':
|
|
23
|
+
pass
|
|
24
|
+
else:
|
|
25
|
+
lists.append(folder+"/"+files)
|
|
26
|
+
get_file(folder+"/"+files,lists)
|
|
27
|
+
else:
|
|
28
|
+
pass
|
|
29
|
+
return lists
|
|
30
|
+
def start():
|
|
31
|
+
b=get_file("kuncache",['kuncache'])
|
|
32
|
+
setup(
|
|
33
|
+
name = confkcws["name"],
|
|
34
|
+
version = confkcws["version"],
|
|
35
|
+
keywords = "kuncache"+confkcws['version'],
|
|
36
|
+
description = confkcws["description"],
|
|
37
|
+
long_description = confkcws["long_description"],
|
|
38
|
+
license = confkcws["license"],
|
|
39
|
+
author = confkcws["author"],
|
|
40
|
+
author_email = confkcws["author_email"],
|
|
41
|
+
maintainer = confkcws["maintainer"],
|
|
42
|
+
maintainer_email = confkcws["maintainer_email"],
|
|
43
|
+
url=confkcws['url'],
|
|
44
|
+
packages = b,
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
install_requires = ['kcredis>=1'], #第三方包
|
|
48
|
+
package_data = {
|
|
49
|
+
'': ['*.html', '*.js','*.css','*.jpg','*.png','*.gif'],
|
|
50
|
+
}
|
|
51
|
+
)
|
|
52
|
+
start()
|