summaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/searcher/CachingSearcher.java
blob: f80d6f2e1eb70eed71c4413045174aa651f111b2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.searcher;

import com.yahoo.component.chain.dependencies.After;
import com.yahoo.component.chain.dependencies.Before;
import com.yahoo.container.QrSearchersConfig;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.prelude.cache.Cache;
import com.yahoo.prelude.cache.QueryCacheKey;
import com.yahoo.search.Searcher;
import com.yahoo.processing.request.CompoundName;
import com.yahoo.search.searchchain.Execution;
import com.yahoo.statistics.Statistics;
import com.yahoo.statistics.Value;

/**
 * A generic caching searcher which caches all passing results.
 *
 * @author vegardh
 */
@After("rawQuery")
@Before("transformedQuery")
public class CachingSearcher extends Searcher {

    private static final CompoundName nocachewrite=new CompoundName("nocachewrite");

    private Cache<QueryCacheKey, Result> cache;
    private Value cacheHitRatio = null;

    public CachingSearcher(QrSearchersConfig config, Statistics manager) {
        long maxSizeBytes = config.com().yahoo().prelude().searcher().CachingSearcher().cachesizemegabytes()*1024*1024;
        long timeToLiveMillis = config.com().yahoo().prelude().searcher().CachingSearcher().timetoliveseconds()*1000;
        long maxEntrySizeBytes = config.com().yahoo().prelude().searcher().CachingSearcher().maxentrysizebytes();
        cache=new Cache<>(maxSizeBytes, timeToLiveMillis, maxEntrySizeBytes, manager);
        initRatio(manager);
    }

    private void initRatio(Statistics manager) {
        cacheHitRatio = new Value("querycache_hit_ratio", manager,
                new Value.Parameters().setNameExtension(false).setLogRaw(false).setLogMean(true));
    }

    private synchronized void cacheHit() {
        cacheHitRatio.put(1.0d);
    }

    private synchronized void cacheMiss() {
        cacheHitRatio.put(0.0d);
    }

    private boolean noCacheWrite(Query query) {
        return query.properties().getBoolean(nocachewrite);
    }

    public Result search(com.yahoo.search.Query query, Execution execution) {
        if (query.getNoCache()) {
            return execution.search(query);
        }
        QueryCacheKey queryKey = new QueryCacheKey(query);
        Result cachedResult=cache.get(queryKey);
        if (cachedResult!=null) {
            cacheHit();
            return cachedResult;
        }
        cacheMiss();
        Query originalQuery = query.clone(); // Need a copy, as cache hash key later on, maybe.
        Result result = execution.search(query);
        execution.fill(result);
        if (!noCacheWrite(query)) {
            queryKey.setQuery(originalQuery); // Because the query member has changed state
            cache.put(queryKey,result);
        }
        return result;
    }

}