Google Apps Engine:Memcache/JCache 仅在过期时间内有效

发布于 2024-08-22 07:36:32 字数 1938 浏览 5 评论 0原文

我在 Google Apps Engine 中使用 jsr107 JCache 来保存网站数据。我的代码如下所示:

    public static String read(String link, boolean UTF8) throws Exception {

    URL url = new URL(link);

    String cached = CacheLogic.instance().get(link);

    if (!StringUtil.isEmpty(cached)) {
     return cached;
    }

    // Here i save the website's context into data

    CacheLogic.instance().put(link, data);

    return data;

    }

我的 CacheLogic:

public class CacheLogic {

 private static CacheLogic singleton;
 private Cache cache;
 private final int TTL = 60;

 public static CacheLogic instance() {

  if (singleton == null) {
   singleton = new CacheLogic();
  }

  return singleton;

 }

 private CacheLogic() {

  Map<String, Integer> props = new HashMap<String, Integer>();
  props.put(GCacheFactory.EXPIRATION_DELTA, TTL);

  try {
   CacheFactory cacheFactory = CacheManager.getInstance().getCacheFactory();
   cache = cacheFactory.createCache(props);
  } catch (CacheException e) {
   e.printStackTrace();
  }

 }

 public void put(String key, String value) {
  cache.remove(key);
  cache.put(key, value);
  CacheStatistics stats = cache.getCacheStatistics();
  System.out.println("[CacheLogic] New entry added. Count=" + stats.getObjectCount());
  System.out.println("[CacheLogic] New entry added. size=" + cache.size());
 }

 public String get(String key) {
  return (String) cache.get(key);
 }

}

我已将缓存过期时间设置为 60 秒,并使用诸如 ' 之类的参数调用 read http://www.google.com/something”或“https://stackoverflow.com/something”。

事情就是这样。

如果在我第一次调用它的 60 秒内始终使用相同的参数调用 read 方法,我总是会获取缓存的数据。完美的。

然而,一旦超过 60 秒,我就会返回 null。我阅读了该网站并将其放入缓存中。下次我调用它时,我会再次得到 null。

看起来,无论键是什么,内存缓存在过期时间结束后都不会保存数据。

我是否以错误的方式使用了 API?

更新1:我只在本地尝试过。

I am using jsr107 JCache in Google Apps Engine to save website data. My code looks like:

    public static String read(String link, boolean UTF8) throws Exception {

    URL url = new URL(link);

    String cached = CacheLogic.instance().get(link);

    if (!StringUtil.isEmpty(cached)) {
     return cached;
    }

    // Here i save the website's context into data

    CacheLogic.instance().put(link, data);

    return data;

    }

My CacheLogic:

public class CacheLogic {

 private static CacheLogic singleton;
 private Cache cache;
 private final int TTL = 60;

 public static CacheLogic instance() {

  if (singleton == null) {
   singleton = new CacheLogic();
  }

  return singleton;

 }

 private CacheLogic() {

  Map<String, Integer> props = new HashMap<String, Integer>();
  props.put(GCacheFactory.EXPIRATION_DELTA, TTL);

  try {
   CacheFactory cacheFactory = CacheManager.getInstance().getCacheFactory();
   cache = cacheFactory.createCache(props);
  } catch (CacheException e) {
   e.printStackTrace();
  }

 }

 public void put(String key, String value) {
  cache.remove(key);
  cache.put(key, value);
  CacheStatistics stats = cache.getCacheStatistics();
  System.out.println("[CacheLogic] New entry added. Count=" + stats.getObjectCount());
  System.out.println("[CacheLogic] New entry added. size=" + cache.size());
 }

 public String get(String key) {
  return (String) cache.get(key);
 }

}

I have set the cache expire time to 60 seconds and I call read with arguments like 'http://www.google.com/something' or 'https://stackoverflow.com/something'.

Heres the thing.

If call read the read method with the same arguments all the time within the 60 seconds i first called it, I always get the cached data. Perfect.

However, once it has gone 60+ seconds, I get null back. I read the website's and put it into the cache. Next time i call it, I get null again.

It seems like the memcache never saves the data after the expire time has ended, no matter the key.

Have i used the API in a incorrect way?

UPDATE 1: I have only tried this locally.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

-残月青衣踏尘吟 2024-08-29 07:36:32

最近读到一篇文章(现在找不到),使用 JCache GCacheFactory.EXPIRATION_DELTA 会使整个缓存过期,您必须使用 用于使单个对象过期的低级 API

Recently read a post (can't find it now) that using JCache GCacheFactory.EXPIRATION_DELTA expires the entire cache and you must use the Low-level API to expire individual objects.

断桥再见 2024-08-29 07:36:32

这是一个实际的例子...

public class CacheLogic {

 private static CacheLogic singleton;
 private Cache cache;
 //private final int TTL = 60;

 public static CacheLogic instance() {

  if (singleton == null) {
   singleton = new CacheLogic();
  }

  return singleton;

 }

 private CacheLogic() {

  //Map<String, Integer> props = new HashMap<String, Integer>();
  //props.put(GCacheFactory.EXPIRATION_DELTA, TTL);

  try {
   CacheFactory cacheFactory = CacheManager.getInstance().getCacheFactory();
   cache = cacheFactory.createCache(/*props*/);
  } catch (CacheException e) {
   e.printStackTrace();
  }

 }

 public void put(String key, String value) {
  cache.remove(key);
  cache.put(key, value);
  CacheStatistics stats = cache.getCacheStatistics();
  System.out.println("[CacheLogic] New entry added. Count=" + stats.getObjectCount());
  System.out.println("[CacheLogic] New entry added. size=" + cache.size());
 }

 public String get(String key) {
  return (String) cache.get(key);
 }

}

默认情况下,存储在缓存中的对象将“尽可能长时间”保留在缓存中。您正在设置保存在缓存中的对象在 60 秒后删除,但令您惊讶的是对象在 60 秒后不可用......

Here is a practical example...

public class CacheLogic {

 private static CacheLogic singleton;
 private Cache cache;
 //private final int TTL = 60;

 public static CacheLogic instance() {

  if (singleton == null) {
   singleton = new CacheLogic();
  }

  return singleton;

 }

 private CacheLogic() {

  //Map<String, Integer> props = new HashMap<String, Integer>();
  //props.put(GCacheFactory.EXPIRATION_DELTA, TTL);

  try {
   CacheFactory cacheFactory = CacheManager.getInstance().getCacheFactory();
   cache = cacheFactory.createCache(/*props*/);
  } catch (CacheException e) {
   e.printStackTrace();
  }

 }

 public void put(String key, String value) {
  cache.remove(key);
  cache.put(key, value);
  CacheStatistics stats = cache.getCacheStatistics();
  System.out.println("[CacheLogic] New entry added. Count=" + stats.getObjectCount());
  System.out.println("[CacheLogic] New entry added. size=" + cache.size());
 }

 public String get(String key) {
  return (String) cache.get(key);
 }

}

By default, objects stored in the cache will remain in the cache for "as long as possible". You are setting objects saved in the cache to be deleted after 60 seconds, and yet it seems to surprise you that objects are unavailable after 60 seconds.....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文