Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
cache in python
#Title of this code #python 2.7.8 from pprint import pprint class SAcache: def __init__(self,size): self.size = size self.__hash = {} self.__queue = [] def put(self,cache_id,data): if self.__hash.__len__() == self.size: self.delete_leasted_accessed() self.__hash[cache_id] = data self.__queue.append(cache_id) def get(self,cache_id): if self.__hash.has_key(cache_id): self.__queue.remove(cache_id) self.__queue.append(cache_id) return self.__hash[cache_id] else: return "invalid cache id, get the cache from the database" def delete_leasted_accessed(self): self.__queue.reverse() item = self.__queue.pop() del self.__hash[item] self.__queue.reverse() c = SAcache(5) c.put("c1","this is c1 data") c.put("c2","this is c1 data") c.put("c3","this is c1 data") c.put("c4","this is c1 data") c.put("c5","this is c1 data") c.put("c6","this is c1 data") pprint(c.get('c6')) pprint(c.get('c1'))
run
|
edit
|
history
|
help
0
PyAddBook
MPS2
Attempt to fork bomb server
1.problem_name_1
Type lala suthar
fb_series
Run
python2 - division -1
irctcbot
binary