1

이 질문에 대한 대답은 분명해 보이지만 완전히 확신해야합니다. 따라서 대답이 명확하고 모호하지 않은 진술을 사용하여 권위있는 참조를 제공 할 수 있다면 그것은 훌륭 할 것입니다. MemcacheService는 근본적으로 싱글 톤입니까

내가

public CollectionResponse<Dog> getDogs(Identification request){ 
    MemcacheService syncCacheDog = MemcacheServiceFactory.getMemcacheService(); 
    syncCacheDog.setErrorHandler(ErrorHandlers.getConsistentLogAndContinue(Level.INFO)); 
    // ........ 
    value = (byte[]) syncCacheDog.get(key); // read from cache 
    if (value == null) { 
     // get value from other source 
     // ........ 

     syncCacheDog.put(key, value); // populate cache 
    } 
    // ........ 
} 

public CollectionResponse<Cat> getCats(Identification request){ 
    MemcacheService syncCacheCat = MemcacheServiceFactory.getMemcacheService(); 
    syncCacheCat.setErrorHandler(ErrorHandlers.getConsistentLogAndContinue(Level.INFO)); 
    // ........ 
    value = (byte[]) syncCacheCat.get(key); // read from cache 
    if (value == null) { 
     // get value from other source 
     // ........ 

     syncCacheCat.put(key, value); // populate cache 
    } 
    // ........ 
} 

이 syncCacheDog 및 syncCacheCat이 같은지도를 가리키는 다음 두 가지 방법을 말해? 또는 나는, 그들이 같은지도를 가리키는 싶은 경우에 나는 두 가지 방법 내부

static MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService(); 

후 사용 syncCache를 작성해야합니까?

반면에 싱글 톤인 경우 두 개의 다른 캐시를 어떻게 유지합니까? 나는. 누군가가 내 방법 중 하나를 복사하여 붙여 넣을 수 있고 네임 스페이스로 작성된 것을 보여 주며 Dog와 같은 특정 객체를 처리하기 위해 제네릭 바이트를 처리하는 대신 표시 할 수 있습니까?

답변

1

예, GAE 및 해당 설명서를 사용한 경험에서 Memcache 서비스는 싱글 톤입니다. 더군다나, 다른 버전의 응용 프로그램은 모두 동일한 캐시를 보게됩니다.

다른 캐시를 유지하려면 일반적으로 접두어를 사용하십시오. 서로 다른 클래스에 대한 고유 한 접두사 세트를 유지하는 것은 상대적으로 쉽습니다. 열거 형 어딘가에 최대 접두어를 유지해야합니다. 이전 접두사 번호를 다시 사용하지 마십시오.

public enum MemcachePrefix { 
    DOGS(1), 
    CATS(2); 
    // Max: 2. 
    public final int value; 
    private MemcachePrefix (int value) {this.value = value;} 
}; 

public class Dog { 
    static final MemcachePrefix MEMCACHE_PREFIX = MemcachePrefix.DOGS; 
}; 

class Main { 
    public static void main (String[] args) { 
    Dog dog = new Dog(); 
    System.out.println (dog.MEMCACHE_PREFIX); 
    } 
} 

Namespaces도 있습니다. 프리픽스를 캐시 키에 수동으로 추가하는 대신 네임 스페이스로 사용할 수 있으므로 GAE가 키 조작을 수행 할 수 있습니다.

관련 문제