-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.py
96 lines (76 loc) · 1.72 KB
/
cache.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import pickle
vpath = 'static/Cloud/4'
c = {}
CACHE_SIZE = 16
s1 = 'string 1'
s2 = 'string 2'
s3 = 'string 3'
s4 = 'string 4'
r1 = 'result 1'
r2 = 'result 2'
r3 = 'result 3'
r4 = 'result 4'
# cache = {s1:[r1,1],s2:[r2,0],s3:[r3,2],s4:[r4,8]}
cache = {}
def getresult(cache,sq):
if(cache.get(sq,None)==None):
return None
else:
print "\nCache[sq] - ",type(cache[sq][0])
return cache[sq][0]
def loadCache(filepath):
with open(filepath, 'rb') as input:
cache = pickle.load(input)
return cache
def saveCache(cache, filepath):
with open(filepath, 'wb') as output:
pickle.dump(cache, output, pickle.HIGHEST_PROTOCOL)
def update(cache,s1,r1):
if(cache.get(s1,None)==None):
if(len(cache)<CACHE_SIZE):
print 'cache not full, new element'
cache[s1] = [r1,0]
else:
print 'cache full, new element'
Max = 0
ind = ''
for item in cache:
if(cache[item][1]>Max):
Max = cache[item][1]
ind = item
del cache[ind]
cache[s1] = [r1,0]
else:
print 'old element'
cache[s1][1]=0
for item in cache:
cache[item][1]+=1
return cache
print '\n----------------------'
update(cache,s1,r1)
print cache
print '\n----------------------'
update(cache,s2,r2)
print cache
print '\n----------------------'
update(cache,s3,r3)
print cache
print '\n----------------------'
update(cache,s1,r1)
print cache
print '\n----------------------'
update(cache,s2,r2)
print cache
print '\n----------------------'
update(cache,s4,r4)
print cache
''' Log Out'''
saveCache(cache,vpath+'/cache.pickle')
cache = {}
print '\nCCR : ',cache
''' Log In'''
cache = loadCache(vpath+'/cache.pickle')
print '\nLC : ',cache
''' Check in Cache'''
answer = getresult(cache,s4)
print '\nAnswer : ',answer