Esempio n. 1
0
def run(mc):
    print('run')

    mc.set('cnt', 0)
    mc.incr('cnt', 2)
    mc.decr('cnt', 5)
    print(mc.get('cnt'))
Esempio n. 2
0
def run(mc):
    print('run')

    # write 2kb entry
    v = 2046 * 'a'
    if not mc.set('test_key', v):
        raise RuntimeError("failed to set value")

    if v != mc.get('test_key'):
        raise RuntimeError("returned value differs")
Esempio n. 3
0
def run(mc):
    print('run')

    # write 2kb entry
    v = 2046 * 'a'
    if not mc.set('test_key', v):
        raise RuntimeError("failed to set value")

    if v != mc.get('test_key'):
        raise RuntimeError("returned value differs")
Esempio n. 4
0
    def display_page_or_304(self, get_page, expiry=PAGE_CACHING_TIME_304, request_path=None):
        """ It just uses etag because last-modified is for http 1.0 and nowadays no browser uses it """
        import mc
        content = None
        #- It's possible to customize the request path in case different requeste paths have the same content -#
        request_path = request_path or self.request.path
        #- Trying to get the etag from cachepy -#
        current_etag = cachepy.get(constants.cachepy.ETAG_KEY % request_path)
        #- There is no etag stored in cachepy.
        if current_etag is None:
            #- Trying to get the etag from mc.
            current_etag = mc.get(constants.mc.ETAG_KEY % request_path)
            if current_etag is None:
                #- MC doesn't have the etag either -#
                content = get_page() # Getting the page content.
                current_etag = self._get_content_etag(content) # Generating etag for the content.
                mc.add(constants.mc.ETAG_KEY % request_path, current_etag, expiry)
                cachepy_expiry = expiry if expiry != 0 else None
                cachepy.set(constants.cachepy.ETAG_KEY % request_path, current_etag, cachepy_expiry)
            else:
                #- MC has the etag let's cache it on cachepy and go on -#
                cachepy.set(constants.cachepy.ETAG_KEY % request_path, current_etag)

        browser_etag = self.request.headers['If-None-Match'] if 'If-None-Match' in self.request.headers else None

        """ Browser etag might be None but current_etag is always generated or gotten from the caches above """

        if browser_etag == current_etag:
            """ Ther user has already the content cached on his browser """
            self.response.headers['ETag'] = current_etag;
            self.error(304)
        else:
            """ No etag match so the content has to be generated and transferred to the client """
            if content is None:
                """ The content wasn't generated above so lest do it now """
                content = get_page()
            """ There is no need to generate the etag again because always is going to be generated above, either taken from cachepy or memcache
            or generated again """
            self.response.headers['ETag'] = current_etag;
            self.response.out.write(content)
Esempio n. 5
0
 def wrapper(*args, **kwargs):
     timestamp = int(time.time())
     self = args[0]
     #- Check if the ip was banned -#
     ip_ban_key_name = constants.mc.IP_BAN % self.request.remote_addr
     ban = mc.get(ip_ban_key_name)
     if ban:
         #- Renew the ban if it's going to expire in 1/3 of the banning time -#
         ban_pending_time = ban_time - (timestamp - ban)
         if ban_pending_time < (ban_time/3):
             #- Renewing the ban because the attacker insists -#
             mc.set(ip_ban_key_name, time.time(), ban_time)
         self.abort(403)
     current_minute = timestamp - (timestamp%60)
     key_name = constants.mc.REQUEST_HIT_FROM_IP % (current_minute, self.request.path, self.request.remote_addr)
     #- Increases hits from an ip to a particular url -#
     counter = mc.incr(key_name)
     if counter > limit_per_minute:
         #- Banning ip -#
         mc.set(ip_ban_key_name, time.time(), ban_time)
         self.abort(403)
     else:
         return fn(*args, **kwargs)
Esempio n. 6
0
def run(mc):
    print('run')

    mc.set('key', 'abc')
    mc.delete('key')
    print(mc.get('key'))
Esempio n. 7
0
def run(mc):
    print('run')
    mc.set('key', 'abc')
    print(mc.get('key'))
    print(mc.get_stats())
Esempio n. 8
0
def run(mc):
    print('run')
    mc.set('key', 'abc')
    print(mc.get('key'))
    print(mc.get_stats())
Esempio n. 9
0
def run(mc):
    print('run')

    mc.set('key', 'abc')
    mc.delete('key')
    print(mc.get('key'))