Is Memcache (Java) for Google App Engine a global cache?

后端 未结 3 1392
借酒劲吻你
借酒劲吻你 2021-02-19 03:27

I\'m new to Google App Engine, and I\'ve spent the last few days building an app using GAE\'s Memcache to store data. Based on my initial findings, it appears as though GAE\'s

3条回答
  •  抹茶落季
    2021-02-19 04:10

    I found the issue and got it working. I was initially using the JCache API and couldn't get it to work, so I switched over to the low-level Memcache API but forgot to remove the old JCache code. So they two implementations were stepping on each other.

    I'm not sure why the JCache implementation didn't work so I'll share the code:

        try {
            if (CacheManager.getInstance().getCache(CACHE_GEO_CLIENTS) == null) {
                Cache cache = CacheManager.getInstance().getCacheFactory().createCache(Collections.emptyMap());
                cache.put(CACHE_GEO_CLIENTS, new HashMap());
                CacheManager.getInstance().registerCache(CACHE_GEO_CLIENTS, cache);
            }
    
        } catch (CacheException e) {
    
            log.severe("Exception while creating cache: " + e);
    
        }
    

    This block of code is inside a private constructor for a singleton called CacheService. This singleton serves as a Cache facade. Note that since requests can be served by different nodes, each node will have this Singleton instance. So when the Singleton is constructed for the first and only time, it'll check to see if my cache is available. If not, it'll create it. This should technically happen only once since Memcache is global yeah? The other somewhat odd thing I'm doing here is creating a single cache entry of type HashMap to store my actual values. I'm doing this because I need to enumerate through all keys and that's something that I can't do with Memcache natively.

    What am I doing wrong here?

提交回复
热议问题