Esempio n. 1
0
def CacheSphinx(cache, cl):
    """Caches the request of a Sphinx client.
    """
    # there are requests and to be computed results
    reqs = [req for req in cl._reqs]
    results = [None] * len(reqs)
    comp_reqs = []
    comp_results = []

    # get results from cache
    for i, req in enumerate(reqs):
        if req in cache:
            results[i] = cache.Get(req)
            results[i]['time'] = 0
        else:
            comp_reqs.append(req)

    # get results that need to be computed
    if comp_reqs:
        cl._reqs = comp_reqs
        comp_results = SphinxClient.RunQueries(cl)
    else:
        cl._reqs = []

    # return None on IO failure
    if comp_results == None:
        return None

    # cache computed results and Get results
    for req, result in zip(comp_reqs, comp_results):
        if result != None:
            cache.Set(req, result)
        results[results.index(None)] = result

    return results
Esempio n. 2
0
 def RunQueries(self, caching=None):
     if not self.cache or caching is False:
         return SphinxClient.RunQueries(self)
     else:
         return cache.CacheSphinx(self.cache, self)