Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Dropwizard中使用缓存?_Java_Mongodb_Caching_Metadata_Dropwizard - Fatal编程技术网

Java 在Dropwizard中使用缓存?

Java 在Dropwizard中使用缓存?,java,mongodb,caching,metadata,dropwizard,Java,Mongodb,Caching,Metadata,Dropwizard,我有一个正在运行的服务,它与MongoDB集成,向MongoDB添加数据并处理分析查询。原始数据是非常非结构化的,并且在存储其人工数据的同时,将其带到单个级别并存储在MongoDB集合中。转换是使用元数据执行的,元数据是这样的: { "DeviceCategory":"DeviceCategoryName", "ObjectIdentifier":"CollectionName" //Collection where document needs to be inserted "Node": [

我有一个正在运行的服务,它与MongoDB集成,向MongoDB添加数据并处理分析查询。原始数据是非常非结构化的,并且在存储其人工数据的同时,将其带到单个级别并存储在MongoDB集合中。转换是使用元数据执行的,元数据是这样的:

{
"DeviceCategory":"DeviceCategoryName",
"ObjectIdentifier":"CollectionName" //Collection where document needs to be inserted
"Node": [
    "node1",
    "node2"   // embeded nodes in raw data
],
"ExtraFields": [   //using this extra fields are added to raw data to handle queries
    {
        "TargetCollection": "TargetCollectionName", //collection to query for document
        "QueryParams": [       //parameter needed to query
            "Param1",
            "Param2"
        ],
        "Keys": [
            {
                "KeyToMap": "KeyName",    //field to extract from returned document
                "TargetKey": "NewKeyName"  //key name to be added with value to KeyToMap
            },
            .....
        ]
    },
    {
        "Collection": "TargetCollectionName",
        "QueryParams": [
            "Param1"                
        ],
        "Keys": [
            {
                "KeyToMap": "KeyName",
                "TargetKey": "NewKeyName"
            },
           ......
        ]
    }
]
 }
我已将此元数据存储在MongoDB的Meta_Data collection中,并针对每个插入请求查询它

我想在服务开始时缓存此数据。我正在dropwizard中寻找一个好的缓存解决方案。我已经阅读了dropwizard文档,但我仍然不清楚如何在dropwizard中使用缓存。我需要一些东西来帮助我开始使用缓存。该服务正在运行,其dropwizard版本为0.6.2


谢谢

最简单的方法是在资源中使用番石榴。如果要在资源级别执行此操作,则需要向资源添加
@Singleton
注释,因为默认情况下它们是请求范围。在资源上存储状态有缺点,您应该了解这些缺点

例如,我从中提取了资源类以使用缓存

此代码是“HelloWorldResource”:

@Path(“/hello world”)
@产生(MediaType.APPLICATION_JSON)
@独生子女
公共类HelloWorldResource{
私有最终字符串模板;
私有最终字符串defaultName;
专用最终原子计数器;
LoadingCache graphs=CacheBuilder.newBuilder()
.最大尺寸(1)
.建造(
新缓存加载程序(){
公共字符串加载(字符串键)引发异常{
//不管钥匙是什么,只要返回“bar”,这是一个玩具示例
返回“bar”;
}
});
公共HelloWorldResource(字符串模板,字符串默认名称){
this.template=模板;
this.defaultName=defaultName;
this.counter=new AtomicLong();
//如果愿意,可以选择在此处初始化缓存。。。。
}
@得到
@定时
公开说sayHello(@QueryParam(“name”)可选名称)引发异常{
返回新语句(counter.incrementAndGet(),
格式(模板、graphs.get(name.or(defaultName));
}
}
@Path("/hello-world")
@Produces(MediaType.APPLICATION_JSON)
@Singleton
public class HelloWorldResource {
    private final String template;
    private final String defaultName;
    private final AtomicLong counter;

    LoadingCache<String, String> graphs = CacheBuilder.newBuilder()
            .maximumSize(1)
            .build(
                    new CacheLoader<String, String>() {
                        public String load(String key) throws Exception {
                            // just return "bar" no matter what the key is, this is a toy example
                            return "bar";
                        }
                    });

    public HelloWorldResource(String template, String defaultName) {
        this.template = template;
        this.defaultName = defaultName;
        this.counter = new AtomicLong();
        //optionally initialize your cache here if you like....
    }

    @GET
    @Timed
    public Saying sayHello(@QueryParam("name") Optional<String> name) throws Exception {
        return new Saying(counter.incrementAndGet(),
                String.format(template, graphs.get(name.or(defaultName))));
    }
}