Solr是基于Lucene的全文搜索服务器。实际上所有的搜索索引文件都是以文件形式存储在磁盘中。数据量到一定程度上,磁盘的IO会影响搜索性能。那么针对这种情况,我们优化的过程中势必需要运用缓存技术。目前,我们熟知的缓存nosql数据库:redis、mongodb、memcache。不过,本文不在这里针对这些nosql数据库做讨论,本文主要是针对solr已经实现的缓存技术做探讨。
1、httpcache
xml配置
我们看到solrconfig.xml文件中的配置:
<httpCaching never304="true" />
<!-- If you include a <cacheControl> directive, it will be used to
generate a Cache-Control header (as well as an Expires header
if the value contains "max-age=")
By default, no Cache-Control header is generated.
You can use the <cacheControl> option even if you have set
never304="true"
-->
<!--
<httpCaching never304="true" >
<cacheControl>max-age=30, public</cacheControl>
</httpCaching>
-->
<!-- To enable Solr to respond with automatically generated HTTP
Caching headers, and to response to Cache Validation requests
correctly, set the value of never304="false"
如果要启用httpcache需要配置,如下:
<httpCaching never304="false" >
<cacheControl>max-age=30, public</cacheControl>
</httpCaching>
max-age:缓存时间,以秒为单位
public:所有资源都应用
- solr内部实现
HttpSolrCall源码片段
HttpCacheHeaderUtil.setCacheControlHeader(this.config, resp, reqMethod);
//判断缓存是否有效
if ((this.config.getHttpCachingConfig().isNever304()) ||
(!HttpCacheHeaderUtil.doCacheHeaderValidation(this.solrReq, this.req, reqMethod, resp)))
{
solrRsp = new SolrQueryResponse();
SolrRequestInfo.setRequestInfo(new SolrRequestInfo(this.solrReq, solrRsp));
execute(solrRsp);
HttpCacheHeaderUtil.checkHttpCachingVeto(solrRsp, resp, reqMethod);
Iterator headers = solrRsp.httpHeaders();
while (headers.ha