def get_winner_list(cls): cachekey = 'WEB_WINNERS_LIST' show_count = 60 winner_list = CacheAdpter.get(cachekey, 'default', []) if not winner_list: winner_list = [] objs = list(UserLottery.objects.filter(lottery_num = cls.lottery_num, awards_type__in = [1, 2, 3, 4, 5, 6]).order_by('-create_time')[:show_count]) if show_count - len(objs) > 0: # 抽奖人数少时,伪造数据 objs = list(Customer.objects.filter(is_b2c = 1).order_by('-create_time')[:show_count]) # 按权重生成 奖项 list awards_list = [v[0] for v in cls.awards_config_dict.values()] awards_list.extend(['免费宝贝拍摄', '100元软件抵价券', '圣诞礼包']) random_cfg_list = [10 for i in range(len(cls.awards_config_dict))] random_cfg_list.extend([5, 7, 1]) new_awards_list = [] for i, awards_desc in enumerate(awards_list): temp_list = [awards_desc for j in range(random_cfg_list[i])] new_awards_list.extend(temp_list) for obj in objs: size = len(obj.nick) if size > 2: new_user_name = '%s%s%s' % (obj.nick[0], '*' * (min(size - 2, 4)), obj.nick[-1]) awards_desc = random.choice(new_awards_list) winner_list.append({'nick': new_user_name, 'awards_desc': awards_desc}) CacheAdpter.set(cachekey, winner_list, 'default', 60 * 5) return winner_list
def load_in_cache_if_not(): flag_dict = CacheAdpter.get(CacheKey.KWLIB_SYNOWORD % -1, 'web', {'init': ''}) if not flag_dict.has_key('init'): return temp_cat_id = -1 word_dict = {} syno_word_list = SynonymWord.objects.all().order_by('cat_id') for sw in syno_word_list: if not sw: continue word_list = sw.word_list.split(',') temp_word_dict = { word.replace('\r', ''): word_list for word in word_list } if sw.cat_id == temp_cat_id: word_dict.update(temp_word_dict) else: CacheAdpter.set(CacheKey.KWLIB_SYNOWORD % temp_cat_id, word_dict, 'web', 60 * 60 * 24 * 7) temp_cat_id = sw.cat_id word_dict = temp_word_dict CacheAdpter.set(CacheKey.KWLIB_SYNOWORD % temp_cat_id, word_dict, 'web', 60 * 60 * 24 * 7) log.info('init all synoword into memcache')
def token(self): """获取淘宝的临时验证码""" if not hasattr(self, '_token'): cachekey = CacheKey.SUBWAY_TOKEN % self.shop_id token = CacheAdpter.get(cachekey, 'web') if not token: token = self._get_subway_token() CacheAdpter.set(cachekey, token, 'web', timeout=60 * 60 * 2) self._token = token return self._token
def __is_mutual(self=None, *args, **kwargs): key_name = CacheKey.SUBWAY_DOWNLOAD_MUTUAL_LOCK % (self.shop_id, dl_type) cached_lock = CacheAdpter.get(key_name, 'web') if cached_lock: # 没有缓存,直接下载 return False else: CacheAdpter.set(key_name, True, 'web', 30 * 60) result = fn(self, *args, **kwargs) CacheAdpter.delete(key_name, 'web') return result