Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.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
Google AppEngine(Java)Memcache不存储数据_Java_Google App Engine_Memcached_Jcache - Fatal编程技术网

Google AppEngine(Java)Memcache不存储数据

Google AppEngine(Java)Memcache不存储数据,java,google-app-engine,memcached,jcache,Java,Google App Engine,Memcached,Jcache,我有这个密码 //net.sf.jsr107cache.Cache, CacheException, CacheFactory, CacheManager all imported private static Cache getCache() { Cache cache = null; try { CacheFactory cacheFactory = CacheManager.getInstance().getCacheFactory();

我有这个密码

//net.sf.jsr107cache.Cache, CacheException, CacheFactory, CacheManager all imported
private static Cache getCache() {

    Cache cache = null;
    try {
        CacheFactory cacheFactory = CacheManager.getInstance().getCacheFactory();
        cache = cacheFactory.createCache(Collections.emptyMap());
    } catch (CacheException e) {
        Logger.getLogger(MemcacheUtil.class.getName()).log(Level.SEVERE, 
                "Unable to obtain a cache instance! \n" + e.getMessage());
    }
    return cache;
}
像这样使用它

public static byte[] fetchFromMemcache(String key) {
    return (byte[]) getCache().get(key);
}

public static void saveInMemcache(String key, byte[] value) {
    getCache().put(key, value);
}

但令人烦恼的是,Memcache中没有存储任何内容,也没有打印任何日志消息。在使用JCache实现缓存之前,我使用了低级API,也遇到了同样的问题。我真的很困惑。Memcache工作前是否需要进行任何特殊设置(在代码中、appengine控制台上等)?Memcache是不是天生就恨我?在代码中是否有我正在做(或没有做)的事情导致了所有这些?拜托,我想知道怎么做。谢谢

在使用低级API使用Memcache之前,不需要设置任何特殊设置(在代码中或appengine控制台上)

我使用这个API没有问题


(如果你愿意,明天我可以给你我用来与memcache成功交换数据的代码。

下面是一个类的示例,该类提供静态方法,使用低级API从memcache进行写/读操作。 优点是我使用了缓存的单实例,提供了更好的响应时间。您正在为每个请求创建工厂和缓存服务的新实例,这基本上不是一个好主意

import java.util.Collection;
import java.util.Map;
import java.util.Set;
import com.google.appengine.api.memcache.MemcacheService;
import com.google.appengine.api.memcache.MemcacheServiceFactory;
import com.google.appengine.api.memcache.Stats;


public class AppMemCache {
private static MemcacheService c = MemcacheServiceFactory.getMemcacheService();

public static boolean containsKey(Object key){
    return c.contains(key);
}


public static Object get(Object key){
    return c.get(key);
}

// returns only the cached key/object pairs 
public static Map<String, Object> getAll(Collection<String> keys){
    return c.getAll(keys);
}

public static void put(Object key, Object value){
    c.put(key, value);
}


public static void putAll( Map<String, Object> values){
    c.putAll(values);
}

public static boolean remove(Object key){
    return c.delete(key);
}

@SuppressWarnings({ "rawtypes", "unchecked" })
public static boolean deleteAll(Collection keys){
    Set deletedKeys = c.deleteAll(keys);
    if(deletedKeys.size() == keys.size())
        return true;
    return false;
}

public static Stats getStatistics(){
    return c.getStatistics();
}
}
import java.util.Collection;
导入java.util.Map;
导入java.util.Set;
导入com.google.appengine.api.memcache.MemcacheService;
导入com.google.appengine.api.memcache.MemcacheServiceFactory;
导入com.google.appengine.api.memcache.Stats;
公共类AppMemCache{
私有静态MemcacheService c=MemcacheServiceFactory.getMemcacheService();
公共静态布尔containsKey(对象键){
返回c.contains(键);
}
公共静态对象get(对象键){
返回c.get(key);
}
//仅返回缓存的键/对象对
公共静态映射getAll(集合键){
返回c.getAll(键);
}
公共静态void put(对象键、对象值){
c、 put(键、值);
}
公共静态void putAll(贴图值){
c、 putAll(价值观);
}
公共静态布尔删除(对象键){
返回c.delete(键);
}
@SuppressWarnings({“rawtypes”,“unchecked”})
公共静态布尔deleteAll(集合键){
设置deletedKeys=c.deleteAll(键);
if(deletedKeys.size()==keys.size())
返回true;
返回false;
}
公共静态Stats getStatistics(){
返回c.getStatistics();
}
}

如果另一个答案不足以满足您的要求,请告诉我。非常感谢。我将尽快试用此代码。顺便说一句,我以前也做过类似的事情,由于沮丧,我每次都恢复获取缓存实例。同时,我上面发布的代码使用JCache(高级API)但是,您的答案直接使用com.google.appengine(低级API)。我也使用此选项,但在它不工作时才恢复到JCache。如果它不起作用,我会给您的代码一个正确的旋转,如果它有效或不起作用,我会在这里报告。谢谢!