Skip to content

Commit

Permalink
debug cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Camelsky committed Aug 8, 2014
1 parent 41b34f1 commit 58891fe
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions dwarf/dau.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def get_cache(self, key):
if time.time() - sttl > 0:
self._kill_cache(key)
return None
logging.debug('got cache: %s %s', key, cache.count())
logging.debug('got cache: %s', key)
self._cut_cache()
return cache
return None
Expand All @@ -65,7 +65,7 @@ def _set_cache(self, key, data, ttl=None):
expireat = time.time() + ttl
val = (data, expireat)
self._cache_dict[key] = val
logging.debug('save cache:%s', key)
logging.debug('save cache: %s %s %s', key, ttl, data and len(data) or 'nil')

def _cut_cache(self):
while len(self._cache_dict) > self._max_cache_lens:
Expand Down Expand Up @@ -102,7 +102,7 @@ def _get_cache(self, key):
return self.cache.get_cache(key)
if self._cache_dict.has_key(key):
cache = self._cache_dict.get(key)
logging.debug('Get cache: %s %s', key, cache.count())
logging.debug('Get cache: %s %s', key, cache.count)
return cache

def _cache(self, key, value):
Expand Down Expand Up @@ -142,18 +142,27 @@ def _make_bitmap(self, day=None, Type='dau'):
dauKey = DAU_KEY.format(month=day.strftime(self.config.MONTH_FORMAT))
else:
dauKey = DAU_KEY.format(date=day.strftime(self.config.DATE_FORMAT))
cache_data = self._get_cache(dauKey)
# cache_data = self._get_cache(dauKey)
# dauBitmap = self._get_cache(dauKey) or dauBitmap
if cache_data is None:
# if cache_data is None or True:
if True:
dauBitmap = Bitmap()
bitsDau = self.REDIS.get(dauKey)
cached = self._get_cache(dauKey)
if cached:
bitsDau = cached
if cached is 'None':
bitsDau = None
else:
logging.debug('no cache:%s',dauKey)
bitsDau = self.REDIS.get(dauKey)
self._cache(dauKey, bitsDau or 'None')
if bitsDau:
dauBitmap.frombytes(bitsDau)
# logging.debug('Init bitmap:Count: %s' % (dauBitmap.count()))
if self.filters:
dauBitmap.filter(self.filters)
# logging.info('Filter bitmap: f-%s b-%s' % (self.filters.count(), dauBitmap.count()))
self._cache(dauKey, dauBitmap)
# self._cache(dauKey, dauBitmap)
else:
dauBitmap = cache_data
logging.debug('_make_bitmap Handler:%s %s - %s Sec' % (day,Type,time.time()-s))
Expand Down Expand Up @@ -192,9 +201,10 @@ def _get_newuser_bitmap(self, day=None, Type='dau'):
day = datetime(day.year, day.month, 1)
hKey = (self.config.dau_keys_conf['newuser'],
day.strftime(self.config.DATE_FORMAT))
dauBitmap = self._get_cache(hKey) or dauBitmap
cached = self._get_cache(hKey)
if cached is None:
# dauBitmap = self._get_cache(hKey) or dauBitmap
# cached = self._get_cache(hKey)
# if cached is None or True:
if True:
offsets = self.REDIS.hget(*hKey)
if not offsets:
return dauBitmap
Expand All @@ -204,7 +214,7 @@ def _get_newuser_bitmap(self, day=None, Type='dau'):
s = time.time()
dauBitmap = Bitmap(bmp) # 生成新的实例,避免被篡改
dauBitmap[:offsets] = False
self._cache(hKey, dauBitmap)
# self._cache(hKey, dauBitmap)
else:
dauBitmap = cached
logging.debug('get nu bitmap: %s Sec' % (time.time()-s))
Expand Down

0 comments on commit 58891fe

Please sign in to comment.