def get(self, parent, update = False, **kwargs): res = Cache.get(COMMENTDB, parent, **kwargs) if not res or update: res = Transaction(COMMENTDB).query(parent = parent, **kwargs) if res == None: res = NoneResult Cache.set(COMMENTDB, parent, res,**kwargs) return None if res == NoneResult else res
def get(self, parent, update = False, **kwargs): order = kwargs.pop('order', None) if not order: order = '-datetime' res = Cache.get(CALENDARDB, parent,order = order, **kwargs) if not res or update: res = Transaction(CALENDARDB).query(parent = parent, order = order, **kwargs) Cache.set(CALENDARDB, parent, res if res else NoneResult, **kwargs) return None if res == NoneResult else res
def insert(self, *cache: Cache): if cache.__len__() == 1: self.db_connection.insert(self.collection, cache[0].to_dict()) elif cache.__len__() > 1: self.db_connection.insert_many( self.collection, list(map(lambda this_cache: this_cache.to_dict(), cache))) else: print("CacheDao#insert: Nothing to insert")
def delete(self, *cache: Cache): if cache.__len__() == 1: self.db_connection.delete_one(self.collection, { "is_valid": cache[0].is_valid, "timestamp": cache[0].timestamp }) elif cache.__len__() > 1: self.db_connection.delete_many( self.collection, { 'timestamp': { "$set": list( map(lambda this_cache: this_cache.timestamp, cache)) } })
def run(self): cache = Cache(self.size,self.strategy) self.gen_frequencies() self.gen_file_list() for timeslot in range(self.timeslot): self.go_time() self.apply_epsilon() for hits in range(self.hits): choice = self.select_file() response = cache.append_item(choice) if(response): self.log.insert_log(response,timeslot+1,'hit',cache.get_file_status()) else: self.log.insert_log(choice,timeslot+1,'miss',cache.get_file_status()) self.log.end_write()
def update(self, *cache: Cache): if cache.__len__() == 1: self.db_connection.find_and_update( self.collection, {'timestamp': cache[0].timestamp}, {"$set": cache[0].to_dict()}) elif cache.__len__() > 1: self.db_connection.update_many( self.collection, { 'timestamp': { "$set": list( map(lambda this_cache: this_cache.timestamp, cache)) } }) else: print("CacheDao#update: Nothing to update")
def get_valid_cache(self) -> Cache: last_available_cache = self._cache_dao.select_one({'is_valid': True}) if last_available_cache is not None: if datetime.fromtimestamp( datetime.timestamp(datetime.now()) - last_available_cache.timestamp).time().hour > 1: last_available_cache.is_valid = False self._cache_dao.update(last_available_cache) return last_available_cache else: return Cache()
def __init__(self, black_hole=None): self._side_length = 8 self._cache = Cache.get_instance() self.field = [[Player.EMPTY] * self._side_length for i in range(self._side_length)] half_length = self._side_length // 2 self.field[half_length - 1][half_length - 1] = self.field[half_length][half_length] = Player.WHITE self.field[half_length - 1][half_length] = self.field[half_length][half_length - 1] = Player.BLACK self.black_hole = black_hole if black_hole is not None: (row, col) = black_hole self.field[row][col] = Player.HOLE
def update_cache(self): self._cache_dao.insert(Cache())
def select_many(self, ftr: dict) -> [Cache]: return list( map(lambda obj: Cache(obj["is_valid"], obj["timestamp"]), self.db_connection.select_many(self.collection, ftr)))
def select_one(self, ftr: dict) -> Cache: cache = self.db_connection.select_one(self.collection, ftr) return Cache().from_dict(cache) if cache is not None else Cache()
def get(self): li = Cache.select().where(Cache.submited == 0).limit(12) for o in li: o.submited = 1 o.save() self.finish(data=dict(li=map(model_to_dict, li)))
def parseInputs(): script, filename = argv txt = open(filename) lines = txt.read().split('\n') first_line = lines[0] (no_videos, no_endpoints, no_requests, no_caches, cache_size) = intTuple(first_line) video_size = intTuple(lines[1]) debug("Video %d" % no_videos) debug("Videos size : ") debug(video_size) videos = [] for i in range(no_videos): videos.append(Video(i, video_size[i])) debug(videos) caches = [] for i in range(no_caches): caches.append(Cache(i, cache_size, [])) debug(caches) idx = 2 # loop endpoint endpoints = [] endpoint_id = 0 while (no_endpoints > 0): (latency_to_dc, no_caches) = intTuple(lines[idx]) idx = idx + 1 latency_to_caches = dict() for i in range(no_caches): (cache_server, latency) = intTuple(lines[idx]) latency_to_caches[cache_server] = latency idx = idx + 1 en = Endpoint(endpoint_id, latency_to_caches, latency_to_dc) endpoints.append(en) endpoint_id = endpoint_id + 1 no_endpoints = no_endpoints - 1 debug(endpoints) requests = [] while (no_requests > 0): (video_id, endpoint_id, reqs) = intTuple(lines[idx]) requests.append(Request(video_id, endpoint_id, reqs)) idx = idx + 1 no_requests = no_requests - 1 debug(requests) idx = 2 return (caches, endpoints, requests, videos)