Exemplo n.º 1
0
    def _cache_next(self):
        now = datetime.now()
        key = self._next_key_gen()
        val = self.get_argument("next", "/")

        c = Cache()
        c.key = key
        c.value = val
        c.expire = now + timedelta(seconds=600)

        value = c.findby_key(key)
        if value:
            c.save(value["_id"])
        else:
            c.insert()
Exemplo n.º 2
0
    def _login_redirect(self, status_):
        key = self._next_key_gen()

        c = Cache()
        value = c.findby_key(key)
        if value:
            next = escape.utf8(value["value"])
            c.remove(value["_id"])
        else:
            next = "/"

        #next = self.get_argument("next", "/")
        if status_ == const.Status.INIT:
            self.redirect("/user/profile?next=%s" % next)
        else:
            self.redirect(next)
Exemplo n.º 3
0
    def wrapper(func, self, *args, **kwargs):
        now = datetime.now()
        if key:
            c = key
        else:
            c = self.__class__.__name__ + func.__name__
        k, handler, cond = key_gen(self, condition, anonymous, c, *args,
                                   **kwargs)

        value = Cache().findby_key(k)
        new_cond = {}

        is_valid = _valid_cache(value, handler, cond, new_cond, anonymous, now)
        if is_valid and value["status"] in (200, 304) and value["chunk"]:
            if new_cond:
                c = Page()
                c.key = k
                c.condition = new_cond["condition"]
                c.save(value["_id"])

            # finish request with cache chunk and headers
            self.set_status(value["status"])
            self.set_header("Content-Type",
                            utf8(value["headers"]["Content-Type"]))
            self.write(utf8("".join(value["chunk"])))
        else:
            func(self, *args, **kwargs)

            c = Page()
            c.key = k
            c.status = self._status_code
            c.headers = self._headers
            c.chunk = self._write_buffer
            c.expire = now + timedelta(seconds=expire)
            c.condition = new_cond.get("condition", "")

            if value:
                c.save(value["_id"])
            else:
                c.insert()
Exemplo n.º 4
0
    def wrapper(func, self, *args, **kwargs):
        now = datetime.now()
        if key:
            c = key
        else:
            c = self.__class__.__name__ + func.__name__
        k, handler, cond = key_gen(self, condition, anonymous, c, *args, **kwargs)

        value = Cache().findby_key(k)
        new_cond = {}

        if _valid_cache(value, handler, cond, new_cond, anonymous, now):
            if new_cond:
                c = Cache()
                c.key = k
                c.condition = new_cond["condition"]
                c.save(value["_id"])
            return value["value"]
        else:
            val = func(self, *args, **kwargs)

            c = Cache()
            # need key, or save will not work
            c.key = k
            c.value = val
            c.expire = now + timedelta(seconds=expire)
            c.condition = new_cond.get("condition", "")

            if value:
                c.save(value["_id"])
            else:
                c.insert()

            return val
Exemplo n.º 5
0
def remove(key):
    """Remove a cache's value."""
    c = Cache()
    v = c.findby_key(key)
    if v:
        c.remove(v["_id"])
Exemplo n.º 6
0
    def wrapper(func, self, *args, **kwargs):
        now = datetime.now()
        if key:
            c = key
        else:
            c = self.__class__.__name__ + func.__name__
        k, handler, cond = key_gen(self, condition, anonymous, c, *args,
                                   **kwargs)

        value = Cache().findby_key(k)
        new_cond = {}

        if _valid_cache(value, handler, cond, new_cond, anonymous, now):
            if new_cond:
                c = Cache()
                c.key = k
                c.condition = new_cond["condition"]
                c.save(value["_id"])
            return value["value"]
        else:
            val = func(self, *args, **kwargs)

            c = Cache()
            # need key, or save will not work
            c.key = k
            c.value = val
            c.expire = now + timedelta(seconds=expire)
            c.condition = new_cond.get("condition", "")

            if value:
                c.save(value["_id"])
            else:
                c.insert()

            return val
Exemplo n.º 7
0
def remove(key):
    """Remove a cache's value."""
    c = Cache()
    v = c.findby_key(key)
    if v:
        c.remove(v["_id"])