def set_lru_read(self, ip, post): #保存别人正在读 lru_views = cache.get('lru_views') print("保存别人正在读取%s"%lru_views) if not lru_views: lru_views = LRUCacheDict(max_size=10, expiration=FIF_MIN) if post not in lru_views.values(): lru_views[ip] = post cache.set('lru_views', lru_views, FIF_MIN) print("set 完成后的%s"%cache.get('lru_views'))
def get_context_data(self,*args,**kwargs): context = super(BaseMixin,self).get_context_data(**kwargs) try: context['categories'] = Category.available_list() context['widgets'] = Widget.available_list() context['recently_posts'] = Post.get_recently_posts(RECENTLY_NUM) context['hot_posts'] = Post.get_hots_posts(HOT_NUM) context['pages'] = Page.objects.filter(status=0) if cache.get('online.ips')!= None: context['online_num'] = len(cache.get('online_ips')) except Exception as e: logger.exception(u'加载基本信息出错[%s]',e) return context
def get(self, request, *args, **kwargs): ip = get_real_ip self.cur_user_ip = ip alias = self.kwargs.get("slug") alias = alias.replace(" ", "") try: self.object = self.queryset.get(alias=alias) except Post.DoesNotExist: referer = request.META.get("HTTP_REFERER") logger.error(u"ref[%s] [%s]访问不存在的文章:[%s]", referer, ip, alias) context = super(PostDetailView, self).get_context_data(**kwargs) return render(request, "404.html", context) visited_ips = cache.get(self.object.id, []) if ip not in visited_ips: Post.objects.filter(id=self.object.id).update(view_times=F("view_times") + 1) visited_ips.append(ip) self.set_lru_read(ip, self.object) DAY = 24 * 60 # 一天 cache.set(self.object.id, visited_ips, DAY) context = self.get_context_data(object=self.object) return self.render_to_response(context)
def get(self, request, *args, **kwargs): if 'HTTP_X_FORWARDED_FOR' in request.META: ip = request.META['HTTP_X_FORWARDED_FOR'] else: ip = request.META['REMOTE_ADDR'] self.cur_user_ip = ip alias = self.kwargs.get('slug') visited_ips = cache.get(alias, []) if ip not in visited_ips: try: post = self.queryset.get(alias=alias) except Post.DoesNotExist: logger.error(u'访问不存在的文章:[%s]' % alias) context = super(PostDetailView, self).get_context_data(**kwargs) return render(request, '404.html', context) else: post.view_times += 1 post.save() visited_ips.append(ip) self.set_lru_read(ip, post) cache.set(alias, visited_ips, FIF_MIN) return super(PostDetailView, self).get(request, *args, **kwargs)
def get_context_data(self, **kwargs): context = super(PostDetailView, self).get_context_data(**kwargs) post = self.get_object() next_id = post.id + 1 prev_id = post.id - 1 try: next_post = self.queryset.get(id=next_id) except Post.DoesNotExist: next_post = None try: prev_post = self.queryset.get(id=prev_id) except Post.DoesNotExist: prev_post = None context['next_post'] = next_post context['prev_post'] = prev_post context['lru_views'] = cache.get('lru_views', {}).items() context['cur_user_ip'] = self.cur_user_ip context['related_posts'] = post.related_posts return context
def get(self, request, *args, **kwargs): """ A base view for displaying a single object """ alias = self.kwargs.get('slug').replace(' ', '') try: self.object = self.queryset.get(alias=alias) except Post.DoesNotExist: referer = request.META.get("HTTP_REFERER") logger.error(u'ref[%s]访问不存在的文章:[%s]', referer, alias) context = super(PostDetailView, self).get_context_data(**kwargs) return render(request, '404.html', context) ip = get_real_ip(request) if ip is None: ip = get_ip(request) visited_ips = cache.get(self.object.id, []) if ip not in visited_ips: Post.objects.filter(id=self.object.id).update( view_times=F('view_times') + 1) visited_ips.append(ip) # set timeout a hour cache.set(self.object.id, visited_ips, 24 * 60) context = self.get_context_data(object=self.object) return self.render_to_response(context)
def process_view(self, request, view_func, view_args, view_kwargs): """ 处理当前在线人数 """ http_user_agent = request.META.get('HTTP_USER_AGENT') if 'Spider' in http_user_agent or 'spider' in http_user_agent: return if 'HTTP_X_FORWARDED_FOR' in request.META: ip = request.META['HTTP_X_FORWARDED_FOR'] else: ip = request.META['REMOTE_ADDR'] # ip should be like '115.23.65.112' # but on SAE the ip is like '115.23.65.112,115.23.65.112' # and that cause an cache error, so I split it. ip = ip.split(',')[0] online_ips = cache.get("online_ips", []) if online_ips: online_ips = cache.get_many(online_ips).keys() cache.set(ip, 0, 5 * 60) if ip not in online_ips: online_ips.append(ip) cache.set("online_ips", online_ips)
def process_view(self, request, view_func, view_args, view_kwargs): """ 处理当前在线人数 整个逻辑很简单,每一个用户访问,我都会把用户的ip作为key放到memcache,然后有一个 online_ips 的key,用来存放所有的ip。 每次请求都会进行如下步骤,先取出 online_ips的所有值,然后再根据这个这个list来从memcache中取出依然存在的ip, 然后再次存入 online_ips 。 """ http_user_agent = request.META.get('HTTP_USER_AGENT') if 'Spider' in http_user_agent or 'spider' in http_user_agent: return if 'HTTP_X_FORWARDED_FOR' in request.META: ip = request.META['HTTP_X_FORWARDED_FOR'] else: ip = request.META['REMOTE_ADDR'] online_ips = cache.get("online_ips", []) if online_ips: online_ips = cache.get_many(online_ips).keys() cache.set(ip, 0, 5 * 60) if ip not in online_ips: online_ips.append(ip) cache.set("online_ips", online_ips)
def get_context_data(self, *args, **kwargs): context = super(BaseMixin, self).get_context_data(**kwargs) try: context['online_num'] = len(memcache.get('online_ips')) except Exception as e: logger.exception(u'加载基本信息出错[%s]!', e) return context
def validate_graph_captcha(self, field): print(field.data) print('test graph captcha') captcha = field.data print(captcha) graph_captcha_mem = cache.get(captcha.lower()) if not graph_captcha_mem: raise ValidationError('图形验证码错误!')
def validate_sms_captcha(self, field): sms_captcha = field.data print('提交的短信验证码是:', sms_captcha) telephone = self.telephone.data sms_captcha_mem = cache.get(telephone) print('取出的短信验证码是:', sms_captcha_mem) if not sms_captcha_mem or sms_captcha.lower() != sms_captcha_mem.lower(): raise ValidationError(message='短信验证码错误!')
def get_context_data(self, **kwargs): context = super(PostDetailView, self).get_context_data(**kwargs) context["lru_views"] = cache.get("lru_views", {}).items() context["cur_user_ip"] = self.cur_user_ip context["related_posts"] = self.object.related_posts return context
def set_lru_read(self, ip, post): # 保存别人正在读 lru_views = cache.get("lru_views") if not lru_views: lru_views = LRUCacheDict(max_size=10, expiration=FIF_MIN) if post not in lru_views.values(): lru_views[ip] = post cache.set("lru_views", lru_views, FIF_MIN)
def graph_captcha(): text, image = Captcha.gene_graph_captcha() print('存的key为:', text.lower()) cache.set(text.lower(), text.lower()) out = BytesIO() image.save(out, 'png') out.seek(0) resp = make_response(out.read()) resp.content_type = 'image/png' print('取出的值为:', cache.get(text.lower())) return resp
def get_context_data(self, **kwargs): context = super(OnlineUserPageView, self).get_context_data(**kwargs) onlineips = memcache.get('online_ips') onlineusers = [] for ip in onlineips: address = iplocater.getIpAddr( string2ip( ip ) ) if '阿里云' in address or '淘宝' in address or '谷歌' in address: continue onlineusers.append((ip,address)) context['onlineusers'] = onlineusers return context
def get_context_data(self, *args, **kwargs): context = super(BaseMixin, self).get_context_data(**kwargs) try: context['categories'] = Category.available_list() context['widgets'] = Widget.available_list() context['recently_posts'] = Post.get_recently_posts(RECENTLY_NUM) context['hot_posts'] = Post.get_hots_posts(HOT_NUM) context['pages'] = Page.objects.filter(status=0) context['online_num'] = len(cache.get('online_ips')) except Exception as e: logger.exception(u'加载基本信息出错[%s]!', e) return context
def POST(self): data = web.input() name = data.get('name') ticket = data.get('ticket') key = key_of_user_grade(name, ticket) score = cache.get(key) if not score: print "cannot get in cache" score = getScore({'xm': name.encode('utf8'), 'zkzh': ticket.encode('utf8')}) print '61:', score if score != "error": cache.set(key, score) print "score:", score return render.chafen(name, ticket, score)
def get_context_data(self, *args, **kwargs): if 'object' in kwargs or 'query' in kwargs: context = super(BaseMixin, self).get_context_data(**kwargs) else: context = {} try: context['categories'] = Category.available_list() context['widgets'] = Widget.available_list() context['recently_posts'] = Post.get_recently_posts(settings.RECENTLY_NUM) context['hot_posts'] = Post.get_hots_posts(settings.HOT_NUM) context['pages'] = Page.objects.filter(status=0) context['online_num'] = len(cache.get('online_ips', [])) except Exception as e: logger.exception(u'加载基本信息出错[%s]!', e) return context
def get_context_data(self, *args, **kwargs): if "object" in kwargs or "query" in kwargs: context = super(BaseMixin, self).get_context_data(**kwargs) else: context = {} try: context["categories"] = Category.available_list() context["widgets"] = Widget.available_list() context["recently_posts"] = Post.get_recently_posts(RECENTLY_NUM) context["hot_posts"] = Post.get_hots_posts(HOT_NUM) context["all_tags"] = Post.all_tags() context["pages"] = Page.objects.filter(status=0) context["online_num"] = len(cache.get("online_ips", [])) except Exception as e: logger.exception(u"加载基本信息出错[%s]!", e) return context
def sms_captcha(): form = SMSCaptchaForm(request.form) if form.validate(): telephone = form.telephone.data captcha = Captcha.gene_text(number=6) print('发送的短信验证码是:', captcha) resp = sms.send(telephone, {'code': captcha}) print(resp.decode()) data = sms.convert(resp) if data == 'OK': cache.set(telephone, captcha) print(cache.get(telephone)) return restful.success() else: return restful.params_error(message='短信验证码发送失败!') else: return restful.params_error(message='参数错误')
def article_detail(request, pk, article_slug): article = get_object_or_404(Article, pk=pk) if article_slug != article.slug: return redirect(article, permanent=True) # add view times if 'HTTP_X_FORWARDED_FOR' in request.META: current_ip = request.META['HTTP_X_FORWARDED_FOR'] else: current_ip = request.META['REMOTE_ADDR'] visited_ips = cache.get(article_slug, []) if current_ip not in visited_ips: article.view_times += 1 article.save() visited_ips.append(current_ip) cache.set(article_slug, visited_ips, 1*60) return render(request, 'blog/article.html', {'article': article})
def wrapped(*args, **kwargs): key = self.get_slot_name(func, args[1:] if is_method(func) else args, kwargs) result = cache.get(key) # If result not in cache - saving if result is None: if self.work_in_background: def do_work(): res = func(*args, **kwargs) cache.set(key, res, self._timeout) IOLoop.current().spawn_callback(do_work) result = self.default_result else: result = func(*args, **kwargs) cache.set(key, result, self._timeout) return result
def process_view(self, request, view_func, view_args, view_kwargs): """ 处理当前在线人数 """ http_user_agent = request.META.get('HTTP_USER_AGENT', []) if 'Spider' in http_user_agent or 'spider' in http_user_agent: return online_ips = cache.get("online_ips", []) if online_ips: online_ips = cache.get_many(online_ips).keys() ip = get_real_ip(request) cache.set(ip, 0, 5 * 60) if ip not in online_ips: online_ips.append(ip) cache.set("online_ips", online_ips)
def process_view(self, request, view_func, view_args, view_kwargs): """ 处理当前在线人数 """ http_user_agent = request.META.get('HTTP_USER_AGENT') if 'Spider' in http_user_agent or 'spider' in http_user_agent: return if 'HTTP_X_FORWARDED_FOR' in request.META: ip = request.META['HTTP_X_FORWARDED_FOR'] else: ip = request.META['REMOTE_ADDR'] online_ips = cache.get("online_ips", []) if online_ips: online_ips = cache.get_many(online_ips).keys() cache.set(ip, 0, 5 * 60) if ip not in online_ips: online_ips.append(ip) cache.set("online_ips", online_ips)
def get(self, request, *args, **kwargs): """ A base view for displaying a single object """ alias = self.kwargs.get('slug').replace(' ', '') try: self.object = self.queryset.get(alias = alias) except Post.DoesNotExist: referer = request.META.get("HTTP_REFERER") logger.error(u'ref[%s]访问不存在的文章:[%s]', referer, alias) context = super(PostDetailView, self).get_context_data(**kwargs) return render(request, '404.html', context) ip = get_real_ip(request) if ip is None: ip = get_ip(request) visited_ips = cache.get(self.object.id, []) if ip not in visited_ips: Post.objects.filter(id=self.object.id).update(view_times = F('view_times') + 1) visited_ips.append(ip) # set timeout a hour cache.set(self.object.id, visited_ips, 24 * 60) context = self.get_context_data(object = self.object) return self.render_to_response(context)
def walkAll(self): self.files = memcache.get("files") if self.files == None: print "cache none files" else: print "cache meet files" return self.files = [] walkitems = os.walk(DiskRootFolder) for root, dirs, files in walkitems: for f in files: if f[-3:] != "jpg": #print os.path.join(root, f) continue pathfile = os.path.join(root, f) self.files.append((pathfile, os.stat(pathfile).st_mtime, detailPicPrefix+pathfile[len(DiskRootFolder):])) self.files.sort(key = lambda l: (l[1], l[0]), reverse = True) memcache.set("files",self.files, 300)
def validate_captcha(self, field): captcha = field.data email = self.email.data captcha_cache = cache.get(email) if not captcha_cache or captcha.lower() != captcha_cache.lower(): raise ValidationError("邮箱验证码错误!")
def select(cls): return cache.get(cls.__objname__)