def cached_query(query, sr):
    """Returns the results from running query. The results are cached and
    only recomputed after 'expire_delta'"""
    query._limit = 150
    query._write_cache = True
    iden = query._iden()

    read_cache = True
    #if query is in the cache, the expire flag is true, and the access
    #time is old, set read_cache = False
    if cache.get(iden) is not None:
        if cache.get(expire_key(sr)):
            access_time = cache.get(access_key(sr))
            if not access_time or datetime.now() > access_time + expire_delta:
                cache.delete(expire_key(sr))
                read_cache = False
    #if the query isn't in the cache, set read_cache to false so we
    #record the access time
    else:
        read_cache = False

    #set access time to the last time the query was actually run (now)
    if not read_cache:
        cache.set(access_key(sr), datetime.now())

    query._read_cache = read_cache
    res = list(query)

    return res
Exemple #2
0
def get_hot(sr):
    q = Link._query(Link.c.sr_id == sr._id,
                    sort = desc('_hot'),
                    write_cache = True,
                    limit = 150)

    iden = q._iden()

    read_cache = True
    #if query is in the cache, the expire flag is true, and the access
    #time is old, set read_cache = False
    if cache.get(iden) is not None:
        if cache.get(expire_key(sr)):
            access_time = cache.get(access_key(sr))
            if not access_time or datetime.now() > access_time + expire_delta:
                cache.delete(expire_key(sr))
                read_cache = False
    #if the query isn't in the cache, set read_cache to false so we
    #record the access time
    else:
        read_cache = False

    if not read_cache:
        cache.set(access_key(sr), datetime.now())
    
    q._read_cache = read_cache
    res = list(q)
    
    #set the #1 link so we can ignore it later. expire after TOP_CACHE
    #just in case something happens and that sr doesn't update
    if res:
        cache.set(top_key(sr), res[0]._fullname, TOP_CACHE)

    return res
Exemple #3
0
 def _other_self(self):
     """Load from the cached version of myself. Skip the local cache."""
     l = cache.get(self._cache_key(), allow_local=False)
     if l and l._id != self._id:
         g.log.error("thing.py: Doppleganger on read: got %s for %s", (l, self))
         cache.delete(self._cache_key())
         return
     return l
Exemple #4
0
 def _other_self(self):
     """Load from the cached version of myself. Skip the local cache."""
     l = cache.get(self._cache_key(), allow_local = False)
     if l and l._id != self._id:
         g.log.error("thing.py: Doppleganger on read: got %s for %s",
                     (l, self))
         cache.delete(self._cache_key())
         return 
     return l
Exemple #5
0
    def _delete_from_db(self):
        """
        Usually Things are soft-deleted, so this should be called rarely, and
        only in cases where you're sure the Thing isn't referenced anywhere else.
        """
        if not self._created:
            return

        tdb.del_thing(self._type_id, self._id)
        cache.delete(thing_prefix(self.__class__.__name__, self._id))
Exemple #6
0
    def _delete_from_db(self):
        """
        Usually Things are soft-deleted, so this should be called rarely, and
        only in cases where you're sure the Thing isn't referenced anywhere else.
        """
        if not self._created:
            return

        tdb.del_thing(self._type_id, self._id)
        cache.delete(thing_prefix(self.__class__.__name__, self._id))
Exemple #7
0
def valid_solution(iden, solution):
    if (not iden or not solution or len(iden) != IDEN_LENGTH
            or len(solution) != SOL_LENGTH
            or solution.upper() != cache.get(str(iden))):
        solution = make_solution()
        cache.set(str(iden), solution, time=300)
        return False
    else:
        cache.delete(str(iden))
        return True
Exemple #8
0
 def _delete(self):
     tdb.del_rel(self._type_id, self._id)
     
     #clear cache
     prefix = thing_prefix(self.__class__.__name__)
     #TODO - there should be just one cache key for a rel?
     cache.delete(prefix + str(self._id))
     #update fast query cache
     cache.set(prefix + str((self._thing1_id,
                             self._thing2_id,
                             self._name)), None)
Exemple #9
0
 def POST_resetpassword(self, res, uid, key, password):
     res._update('status', innerHTML = '')
     if res._chk_error(errors.BAD_PASSWORD):
         res._focus('passwd')
     elif res._chk_error(errors.BAD_PASSWORD_MATCH):
         res._focus('passwd2')
     else:
         user = Account._byID(uid, data=True)
         change_password(user, user.password, password)
         cache.delete(str('reset_%s' % key))
         self._login(res, user, '/resetpassword')
Exemple #10
0
        def _delete(self):
            tdb.del_rel(self._type_id, self._id)

            # clear cache
            prefix = thing_prefix(self.__class__.__name__)
            # TODO - there should be just one cache key for a rel?
            cache.delete(prefix + str(self._id))
            # update fast query cache
            cache.set(prefix + str((self._thing1_id, self._thing2_id, self._name)), None)
            # temporarily set this property so the rest of this request
            # know it's deleted. save -> unsave, hide -> unhide
            self._name = "un" + self._name
Exemple #11
0
def valid_solution(iden, solution):
    if (not iden
        or not solution
        or len(iden) != IDEN_LENGTH
        or len(solution) != SOL_LENGTH
        or solution.upper() != cache.get(str(iden))): 
        solution = make_solution()
        cache.set(str(iden), solution, time = 300)
        return False
    else:
        cache.delete(str(iden))
        return True
Exemple #12
0
 def _delete(self):
     tdb.del_rel(self._type_id, self._id)
     
     #clear cache
     prefix = thing_prefix(self.__class__.__name__)
     #TODO - there should be just one cache key for a rel?
     cache.delete(prefix + str(self._id))
     #update fast query cache
     cache.set(prefix + str((self._thing1_id,
                             self._thing2_id,
                             self._name)), None)
     #temporarily set this property so the rest of this request
     #know it's deleted. save -> unsave, hide -> unhide
     self._name = 'un' + self._name
Exemple #13
0
 def _uncache(cls, thing1, thing2, name):
     # Remove a rel from from the fast query cache
     prefix = thing_prefix(cls.__name__)
     cache.delete(prefix + str((thing1._id,
                                thing2._id,
                                name)))
Exemple #14
0
def clear_memo(iden, *a, **kw):
    key = iden + str(a) + str(kw)
    #print 'CLEARING', key
    cache.delete(key)
Exemple #15
0
 def _uncache(cls, thing1, thing2, name):
     # Remove a rel from from the fast query cache
     prefix = thing_prefix(cls.__name__)
     cache.delete(prefix + str((thing1._id,
                                thing2._id,
                                name)))
Exemple #16
0
def clear_memo(iden, *a, **kw):
    key = iden + str(a) + str(kw)
    #print 'CLEARING', key
    cache.delete(key)
Exemple #17
0
def clear_memo(iden, *a, **kw):
    key = _make_key(iden, a, kw)
    #print 'CLEARING', key
    cache.delete(key)
Exemple #18
0
def clear_memo(iden, *a, **kw):
    from r2.config import cache

    key = iden + str(a) + str(kw)
    # print 'CLEARING', key
    cache.delete(key)
Exemple #19
0
def clear_memo(iden, *a, **kw):
    from r2.config import cache
    key = iden + str(a) + str(kw)
    #print 'CLEARING', key
    cache.delete(key)