问题描述
嗨..这个缓存技术的新手。我的项目包含视频,大量图片和一些文本数据。我从数据库中获取所有这些数据。加载数据需要更多时间。如何为我的webapplication实现缓存。我用谷歌搜索缓存,但我没有说明如何将查询放在缓存中。请建议我实现缓存的方法。我使用struts.jsp和mysql数据库来实现web应用程序。
在此先感谢
Hi.. Am new to this cache technique. My project contains videos , lot of images and some text data. Am getting all these data from database. It takes more time to load the data. how to implement cache for my webapplication. I googled for caching but i didnot understang how to put the queries in cache. please suggest me the way to implement cache. Am using struts.jsp and mysql Database for implementing web application.
Thanks In Advance
推荐答案
public Content getContent(Integer id)
{
Content c;
// first look for it in the cache defined as Map<integer,>
if (cache.containsKey(id))
{
c = cache.get(id);
}
else
{
c = queryContent(id);
cache.put(id, c);
}
return c;
}
private Content queryContent(Integer id)
{
// actually go to the DB now and populate the content
return contentFromDb;
}
然而这有问题。你最终在记忆中拥有一切。如果您正在考虑大量数据,那么您需要考虑刷新缓存。通常你会在一个单独的线程中执行此操作,只需浏览缓存中的所有项目,然后删除最旧的项目或将大小减小到特定限制。
这些方法中的任何一种都需要标记缓存中的内容,我使用年龄并请求我使用公式实现它,如果平均调用超过给定的分钟数,则保留所请求的任何内容:
This however has a problem. You eventually have EVERYTHING in memory. If you are considering a large amount of data then you'll need to consider flushing the cache. Normally you'd do this in a separate thread just going through all the items in the cache and either removing the oldest ones or reducing the size to a particular limit.
Either of these approaches need the content in the cache to be stamped, I use age and requests where I have implemented this with a formula that keeps anything requested if on average it is being called more than a given number of minutes:
class Cache
{
// stuff
void flushCache(int maxMinutes)
{
List<integer> remove = HashList<>();
for (Content c : cache.values())
{
if (c.isActive(maxMinites))
{
remove.add(c.getId());
}
}
for (Integer i : remove)
{
cache.remove(i);
}
}
}
class Content
{
bool isActive((int maxMinutes)
{
// age = minutes since instantiated
// calls = number of times content was accessed
if ((age / calls) <= maxMinutes)
{
return true;
}
return false;
}</integer>
现在,当内容不再使用时,它会被慢慢删除。
Now, when the content is no longer not in use it is slowly removed.
这篇关于如何在java Web应用程序中实现缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!