Search in sources :

Example 1 with MemcacheService

use of com.google.appengine.api.memcache.MemcacheService in project java-docs-samples by GoogleCloudPlatform.

the class MemcacheBestPracticeServlet method doGet.

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
    String path = req.getRequestURI();
    if (path.startsWith("/favicon.ico")) {
        // ignore the request for favicon.ico
        return;
    }
    MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
    syncCache.setErrorHandler(ErrorHandlers.getConsistentLogAndContinue(Level.INFO));
    byte[] whoKey = "who".getBytes();
    byte[] countKey = "count".getBytes();
    byte[] who = (byte[]) syncCache.get(whoKey);
    String whoString = who == null ? "nobody" : new String(who);
    resp.getWriter().print("Previously incremented by " + whoString + "\n");
    syncCache.put(whoKey, "Java".getBytes());
    Long count = syncCache.increment(countKey, 1L, 0L);
    resp.getWriter().print("Count incremented by Java = " + count + "\n");
}
Also used : MemcacheService(com.google.appengine.api.memcache.MemcacheService)

Example 2 with MemcacheService

use of com.google.appengine.api.memcache.MemcacheService in project java-docs-samples by GoogleCloudPlatform.

the class MemcacheSyncCacheServlet method doGet.

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
    String path = req.getRequestURI();
    if (path.startsWith("/favicon.ico")) {
        // ignore the request for favicon.ico
        return;
    }
    MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
    syncCache.setErrorHandler(ErrorHandlers.getConsistentLogAndContinue(Level.INFO));
    String key = "count-sync";
    byte[] value;
    long count = 1;
    value = (byte[]) syncCache.get(key);
    if (value == null) {
        value = BigInteger.valueOf(count).toByteArray();
        syncCache.put(key, value);
    } else {
        // Increment value
        count = new BigInteger(value).longValue();
        count++;
        value = BigInteger.valueOf(count).toByteArray();
        // Put back in cache
        syncCache.put(key, value);
    }
    // Output content
    resp.setContentType("text/plain");
    resp.getWriter().print("Value is " + count + "\n");
}
Also used : MemcacheService(com.google.appengine.api.memcache.MemcacheService) BigInteger(java.math.BigInteger)

Example 3 with MemcacheService

use of com.google.appengine.api.memcache.MemcacheService in project community by GoogleCloudPlatform.

the class MemcacheBestPracticeServlet method doGet.

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
    String path = req.getRequestURI();
    if (path.startsWith("/favicon.ico")) {
        // ignore the request for favicon.ico
        return;
    }
    MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
    syncCache.setErrorHandler(ErrorHandlers.getConsistentLogAndContinue(Level.INFO));
    byte[] whoKey = "who".getBytes();
    byte[] countKey = "count".getBytes();
    byte[] who = (byte[]) syncCache.get(whoKey);
    String whoString = who == null ? "nobody" : new String(who);
    resp.getWriter().print("Previously incremented by " + whoString + "\n");
    syncCache.put(whoKey, "Java".getBytes());
    Long count = syncCache.increment(countKey, 1L, 0L);
    resp.getWriter().print("Count incremented by Java = " + count + "\n");
}
Also used : MemcacheService(com.google.appengine.api.memcache.MemcacheService)

Example 4 with MemcacheService

use of com.google.appengine.api.memcache.MemcacheService in project java-docs-samples by GoogleCloudPlatform.

the class MemcacheConcurrentServlet method doGet.

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
    String path = req.getRequestURI();
    if (path.startsWith("/favicon.ico")) {
        // ignore the request for favicon.ico
        return;
    }
    String key = "count-concurrent";
    // Using the synchronous cache.
    MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
    // Write this value to cache using getIdentifiable and putIfUntouched.
    for (long delayMs = 1; delayMs < 1000; delayMs *= 2) {
        IdentifiableValue oldValue = syncCache.getIdentifiable(key);
        byte[] newValue = oldValue == null ? BigInteger.valueOf(0).toByteArray() : // newValue depends on old value
        increment((byte[]) oldValue.getValue());
        resp.setContentType("text/plain");
        resp.getWriter().print("Value is " + new BigInteger(newValue).intValue() + "\n");
        if (oldValue == null) {
            // Key doesn't exist. We can safely put it in cache.
            syncCache.put(key, newValue);
            break;
        } else if (syncCache.putIfUntouched(key, oldValue, newValue)) {
            // newValue has been successfully put into cache.
            break;
        } else {
            // Wait a while before trying again, waiting longer on successive loops.
            try {
                Thread.sleep(delayMs);
            } catch (InterruptedException e) {
                throw new ServletException("Error when sleeping", e);
            }
        }
    }
}
Also used : ServletException(javax.servlet.ServletException) MemcacheService(com.google.appengine.api.memcache.MemcacheService) IdentifiableValue(com.google.appengine.api.memcache.MemcacheService.IdentifiableValue) BigInteger(java.math.BigInteger)

Example 5 with MemcacheService

use of com.google.appengine.api.memcache.MemcacheService in project java-docs-samples by GoogleCloudPlatform.

the class MultitenancyServlet method doGet.

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    String namespace;
    PrintWriter out = resp.getWriter();
    out.println("Code Snippets -- not yet fully runnable as an app");
    // [START temp_namespace]
    // Set the namepace temporarily to "abc"
    String oldNamespace = NamespaceManager.get();
    NamespaceManager.set("abc");
    try {
    // ... perform operation using current namespace ...
    } finally {
        NamespaceManager.set(oldNamespace);
    }
    // [START per_user_namespace]
    if (com.google.appengine.api.NamespaceManager.get() == null) {
        // Assuming there is a logged in user.
        namespace = UserServiceFactory.getUserService().getCurrentUser().getUserId();
        NamespaceManager.set(namespace);
    }
    // [END per_user_namespace]
    String value = "something here";
    // [START ns_memcache]
    // Create a MemcacheService that uses the current namespace by
    // calling NamespaceManager.get() for every access.
    MemcacheService current = MemcacheServiceFactory.getMemcacheService();
    // stores value in namespace "abc"
    oldNamespace = NamespaceManager.get();
    NamespaceManager.set("abc");
    try {
        // stores value in namespace “abc”
        current.put("key", value);
    } finally {
        NamespaceManager.set(oldNamespace);
    }
    // [END ns_memcache]
    // [START specific_memcache]
    // Create a MemcacheService that uses the namespace "abc".
    MemcacheService explicit = MemcacheServiceFactory.getMemcacheService("abc");
    // stores value in namespace "abc"
    explicit.put("key", value);
    // [END specific_memcache]
    // [START searchns]
    // Set the current namespace to "aSpace"
    NamespaceManager.set("aSpace");
    // Create a SearchService with the namespace "aSpace"
    SearchService searchService = SearchServiceFactory.getSearchService();
    // Create an IndexSpec
    IndexSpec indexSpec = IndexSpec.newBuilder().setName("myIndex").build();
    // Create an Index with the namespace "aSpace"
    Index index = searchService.getIndex(indexSpec);
    // [END searchns]
    // [START searchns_2]
    // Create a SearchServiceConfig, specifying the namespace "anotherSpace"
    SearchServiceConfig config = SearchServiceConfig.newBuilder().setNamespace("anotherSpace").build();
    // Create a SearchService with the namespace "anotherSpace"
    searchService = SearchServiceFactory.getSearchService(config);
    // Create an IndexSpec
    indexSpec = IndexSpec.newBuilder().setName("myindex").build();
    // Create an Index with the namespace "anotherSpace"
    index = searchService.getIndex(indexSpec);
// [END searchns_2]
}
Also used : MemcacheService(com.google.appengine.api.memcache.MemcacheService) IndexSpec(com.google.appengine.api.search.IndexSpec) SearchService(com.google.appengine.api.search.SearchService) Index(com.google.appengine.api.search.Index) SearchServiceConfig(com.google.appengine.api.search.SearchServiceConfig) PrintWriter(java.io.PrintWriter)

Aggregations

MemcacheService (com.google.appengine.api.memcache.MemcacheService)10 BigInteger (java.math.BigInteger)4 IdentifiableValue (com.google.appengine.api.memcache.MemcacheService.IdentifiableValue)2 PrintWriter (java.io.PrintWriter)2 ServletException (javax.servlet.ServletException)2 Entity (com.google.appengine.api.datastore.Entity)1 Key (com.google.appengine.api.datastore.Key)1 Index (com.google.appengine.api.search.Index)1 IndexSpec (com.google.appengine.api.search.IndexSpec)1 SearchService (com.google.appengine.api.search.SearchService)1 SearchServiceConfig (com.google.appengine.api.search.SearchServiceConfig)1 StringWriter (java.io.StringWriter)1 Test (org.junit.Test)1