Example #1
0
    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)
Example #2
0
    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)
Example #3
0
    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)
Example #4
0
    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)
Example #5
0
    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)
Example #6
0
    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)
Example #7
0
    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)
Example #8
0
    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)
Example #9
0
    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)
Example #10
0
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
Example #11
0
 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)
Example #12
0
    def get(self, request):
        expire_day = request.GET.get("expire_day")
        if not expire_day:
            expire_day = 7
        time_stamp = int(time.time())
        SALT = "闷声发大财"
        hl = hashlib.md5()
        base = SALT + str(time_stamp)
        hl.update(base.encode("utf8"))
        code = hl.hexdigest()

        # store into redis
        cache.set(code, 'ALL', expire_day*24*3600)
        data = {"code": code}
        return self.success(data)
Example #13
0
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='参数错误')
Example #14
0
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
Example #16
0
    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)
Example #17
0
    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)
Example #18
0
    async def get_tts(self, message, text, lang):
        cache_mp3 = cache.get(text, lang, message.id)
        if not cache_mp3:
            make_tts_func = make_func(self.make_tts, text, lang)
            temp_store_for_mp3 = await self.bot.loop.run_in_executor(
                None, make_tts_func)

            try:
                temp_store_for_mp3.seek(0)
                file_length = int(MP3(temp_store_for_mp3).info.length)
            except HeaderNotFoundError:
                return

            # Discard if over 30 seconds
            if file_length <= 30:
                temp_store_for_mp3.seek(0)
                temp_store_for_mp3 = temp_store_for_mp3.read()

                self.bot.queue[message.guild.id][
                    message.id] = temp_store_for_mp3
                cache.set(text, lang, message.id, temp_store_for_mp3)
        else:
            self.bot.queue[message.guild.id][message.id] = cache_mp3
Example #19
0
    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)
Example #20
0
    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)
Example #21
0
    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)
Example #22
0
    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 update_cache(self, func, new_value, *args, **kwargs):
     key = self.get_slot_name(func, args, kwargs)
     if self.is_sync:
         cache.set(key, new_value)
     else:
         async_cache.set(key, new_value)
 def do_work():
     res = func(*args, **kwargs)
     cache.set(key, res, self._timeout)