What is Memcached

Memcached is a general-purpose distributed memory caching system. It is often used to speed up dynamic database-driven websites by caching data and objects in RAM to reduce the number of times an external data source (such as a database or API) must be read.

Memcached is APIs provide a very large hash table distributed across multiple machines. When the table is full, subsequent inserts cause older data to be purged in least recently used order. Applications using Memcached typically layer requests and additions into RAM before falling back on a slower backing store, such as a database.

When to use a memory cache

One use of a memory cache is to speed up common datastore queries. If many requests make the same query with the same parameters, and changes to the results do not need to appear on the web site right away, the application can cache the results in the memcache. Subsequent requests can check the memcache, and only perform the datastore query if the results are absent or expired. Session data, user preferences, and other data returned by queries for web pages are good candidates for caching.

Memcache can be useful for other temporary values. However, when considering whether to store a value solely in the memcache and not backed by other persistent storage, be sure that your application behaves acceptably when the value is suddenly not available. Values can expire from the memcache at any time, and can be expired prior to the expiration deadline set for the value. For example, if the sudden absence of a user is session data would cause the session to malfunction, that data should probably be stored in the datastore in addition to the memcache.

Service levels

App Engine supports two levels of the memcache service:

Shared memcache is the free default for App Engine applications. It provides cache capacity on a best-effort basis and is subject to the overall demand of all the App Engine applications using the shared memcache service.

Dedicated memcache provides a fixed cache capacity assigned exclusively to your application. It is billed by the GB-hour of cache size and requires billing to be enabled. Having control over cache size means your app can perform more predictably and with fewer reads from more costly durable storage.

Memcached Limits

The following limits apply to the use of the memcache service:

1. The maximum size of a cached data value is 1 MiB (2^20 bytes) minus the size of the key minus an implementation-dependent overhead, which is approximately 73 bytes.

2. A key cannot be larger than 250 bytes. In the Java runtime, keys that are objects or strings longer than 250 bytes will be hashed. (Other runtimes behave differently.)

3. The multi batch operations can have any number of elements. The total size of the call and the total size of the data fetched must not exceed 32 megabytes.

4. A memcache key cannot contain a null byte.

Available APIs

App Engine memcache supports two interfaces: a low-level Memcache API and the JCache specification. The following sections provide more information about each interface.

Low-level API

The low-level Memcache API supports more functionality compared with JCache. Examples include:

1. Atomically increment and decrement integer counter values.

2. Expose more cache statistics, such as the amount of time since the least- recently-used entry was accessed, and the total size of all items in the cache.

3. Check and set operations to conditionally store data.

4. Perform memcache operations asynchronously, using the AsyncMemcacheService.

5. The low-level API provides MemcacheService and AsyncMemcacheService for accessing memcache service. This API is richer than the one provided by JCache.

Memcache JCache

The App Engine Java SDK supports the JCache API. JCache provides a map-like interface to cached data. You store and retrieve values in the cache using keys. Keys and values can be of any Serializable type or class. For more details, see Using Memcache.

JCache features not supported

JCache does not support the following features:

1. The JCache listener API is partially supported for listeners that can execute during the processing of a app is API call, such as for onPut and onRemove listeners. Listeners that require background processing, like onEvict, are not supported.

2. An app can test whether the cache contains a given key, but it cannot test whether the cache contains a given value (containsValue() is not supported).

3. An app cannot dump the contents of the cache is keys or values.

4. An app cannot manually reset cache statistics.

5. The put() method does not return the previous known value for a key. It always returns null.

How cached data expires

Memcache contains key/value pairs. The pairs in memory at any time change as items are written and retrieved from the cache.

By default, values stored in memcache are retained as long as possible. Values can be evicted from the cache when a new value is added to the cache and the cache is low on memory. When values are evicted due to memory pressure, the least recently used values are evicted first.

The app can provide an expiration time when a value is stored, as either a number of seconds relative to when the value is added, or as an absolute Unix epoch time in the future (a number of seconds from midnight January 1, 1970). The value is evicted no later than this time, though it can be evicted earlier for other reasons.

Under rare circumstances, values can also disappear from the cache prior to expiration for reasons other than memory pressure. While memcache is resilient to server failures, memcache values are not saved to disk, so a service failure can cause values to become unavailable.

In general, an application should not expect a cached value to always be available.

Best practices

Following are some best practices for using memcache:

1. Handle memcache API failures gracefully. Memcache operations can fail for various reasons. Applications should be designed to catch failed operations without exposing these errors to end users. This guidance applies especially to Set operations.

2. Use the batching capability of the API when possible, especially for small items. Doing so increases the performance and efficiency of your app.

3. Distribute load across your memcache keyspace. Having a single or small set of memcache items represent a disproportionate amount of traffic will hinder your app from scaling. This guidance applies to both operations/sec and bandwidth. You can often alleviate this problem by explicitly sharding your data.

For example, you can split a frequently updated counter among several keys, reading them back and summing only when you need a total. Likewise, you can split a 500K piece of data that must be read on every HTTP request across multiple keys and read them back using a single batch API call. (Even better would be to cache the value in instance memory.) For dedicated memcache, the peak access rate on a single key should be 1-2 orders of magnitude less than the per-GB rating.

Using JCache

The App Engine Java SDK supports the JCache interface (JSR 107) for accessing memcache. The interface is included in the javax.cache package.

With JCache, you can set and get values, control how values expire from the cache, inspect the contents of the cache, and get statistics about the cache. You can also use listeners to add custom behavior when setting and deleting values.

The App Engine implementation tries to implement a loyal subsetof the JCache API standard. (For more information about JCache, see JSR 107.) However, instead of using JCache, you may want to consider using the low-level Memcache API to access more features of the underlying service.

Getting a cache instance

You use an implementation of the javax.cache.Cache interface to interact with the cache. You obtain a Cache instance using a CacheFactory, which you obtain from a static method on the CacheManager. The following code gets a Cache instance with the default configuration:

import java.util.Collections;
import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheFactory;
import javax.cache.CacheManager;

// ...
        Cache cache;
        try {
            CacheFactory cacheFactory = CacheManager.getInstance().getCacheFactory();
            cache = cacheFactory.createCache(Collections.emptyMap());
        } catch (CacheException e) {
            // ...
        }

The CacheFactory is createCache() method takes a Map of configuration properties. These properties are discussed below. To accept the defaults, give the method an empty Map.

App Engine Documentation Java Standard Environment Using Memcache

This page describes how to configure and monitor the memcache service for your application using the Google Cloud Platform Console. It also describes how to do common tasks using the JCache interface and how to handle concurrent writes using the low-level App Engine memcache API for Java.

App Engine Documentation Java Standard Environment Using Memcache

This page describes how to configure and monitor the memcache service for your application using the Google Cloud Platform Console. It also describes how to do common tasks using the JCache interface and how to handle concurrent writes using the low-level App Engine memcache API for Java.

App Engine Documentation Java Standard Environment Using Memcache

This page describes how to configure and monitor the memcache service for your application using the Google Cloud Platform Console. It also describes how to do common tasks using the JCache interface and how to handle concurrent writes using the low-level App Engine memcache API for Java.

Configuring memcache

Go to the Memcache page in the Google Cloud Platform Console.

GO TO THE MEMCACHE PAGE

Select the memcache service level you want to use:

Shared (default) - free and provides cache capacity on a best-effort basis.

Dedicated - billed by the GB-hour of cache size and provides a fixed cache capacity assigned exclusively to your application.

Learn more about available service classes in Memcache Overview.

Using JCache

The App Engine Java SDK supports the JCache interface (JSR 107) for accessing memcache. The interface is included in the javax.cache package.

With JCache, you can set and get values, control how values expire from the cache, inspect the contents of the cache, and get statistics about the cache. You can also use listeners to add custom behavior when setting and deleting values.

The App Engine implementation tries to implement a loyal subsetof the JCache API standard. (For more information about JCache, see JSR 107.) However, instead of using JCache, you may want to consider using the low-level Memcache API to access more features of the underlying service.

Getting a cache instance

You use an implementation of the javax.cache.Cache interface to interact with the cache. You obtain a Cache instance using a CacheFactory, which you obtain from a static method on the CacheManager. The following code gets a Cache instance with the default configuration:

import java.util.Collections;
import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheFactory;
import javax.cache.CacheManager;

// ...
        Cache cache;
        try {
            CacheFactory cacheFactory = CacheManager.getInstance().getCacheFactory();
            cache = cacheFactory.createCache(Collections.emptyMap());
        } catch (CacheException e) {
            // ...
        }
		

The CacheFactory is createCache() method takes a Map of configuration properties. These properties are discussed below. To accept the defaults, give the method an empty Map.

Putting and getting values

The Cache behaves like a Map: You store keys and values using the put() method, and retrieve values using the get() method. You can use any Serializable object for either the key or the value.

String key; // ... byte[] value; // ... // Put the value into the cache. cache.put(key, value); // Get the value from the cache. value = (byte[]) cache.get(key);

To put multiple values, you can call the putAll() method with a Map as its argument.

To remove a value from the cache (to evict it immediately), call the remove() method with the key as its argument. To remove every value from the cache for the application, call the clear() method.

The containsKey() method takes a key, and returns a boolean (true or false) to indicate whether a value with that key exists in the cache. The isEmpty() method tests whether the cache is empty. The size() method returns the number of values currently in the cache.

Configuring expiration

By default, all values remain in the cache as long as possible, until evicted due to memory pressure, removed explicitly by the app, or made unavailable for another reason (such as an outage). The app can specify a expiration time for values, a maximum amount of time the value will be available. The expiration time can be set as an amount of time relative to when the value is set, or as an absolute date and time.

You specify the expiration policy using configuration properties when you create the Cache instance. All values put with that instance use the same expiration policy. For example, to configure a Cache instance to expire values one hour (3,600 seconds) after they are set:


import java.util.HashMap;
import java.util.Map;
import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheFactory;
import javax.cache.CacheManager;
import javax.concurrent.TimeUnit;
import com.google.appengine.api.memcache.jsr107cache.GCacheFactory;

// ...
        Cache cache;
        try {
            CacheFactory cacheFactory = CacheManager.getInstance().getCacheFactory();
            Map properties = new HashMap<>();
            properties.put(GCacheFactory.EXPIRATION_DELTA, TimeUnit.HOURS.toSeconds(1));
            cache = cacheFactory.createCache(properties);
        } catch (CacheException e) {
            // ...
        }
	

The following properties control value expiration:

GCacheFactory.EXPIRATION_DELTA: expire values the given amount of time relative to when they are put, as an Integer number of seconds

GCacheFactory.EXPIRATION_DELTA_MILLIS: expire values the given amount of time relative to when they are put, as an Integer number of milliseconds

GCacheFactory.EXPIRATION: expire values at the given date and time, as a java.util.Date

Configuring the set policy

By default, setting a value in the cache adds the value if there is no value with the given key, and replaces a value if there is a value with the given key. You can configure the cache to only add (protect existing values) or only replace values (do not add).

import java.util.HashMap;
import java.util.Map;
import com.google.appengine.api.memcache.MemcacheService;

// ...
        Map properties = new HashMap<>();
        properties.put(MemcacheService.SetPolicy.ADD_ONLY_IF_NOT_PRESENT, true);

The following properties control the set policy:

MemcacheService.SetPolicy.SET_ALWAYS: add the value if no value with the key exists, replace an existing value if a value with the key exists; this is the default

MemcacheService.SetPolicy.ADD_ONLY_IF_NOT_PRESENT: add the value if no value with the key exists, do nothing if the key exists

MemcacheService.SetPolicy.REPLACE_ONLY_IF_PRESENT: do nothing if no value with the key exists, replace an existing value if a value with the key exists

Retrieving cache statistics

The app can retrieve statistics about its own use of the cache. These statistics are useful for monitoring and tuning cache behavior. You access statistics using a CacheStatistics object, which you get by calling the Cache is getCacheStatistics() method.

Available statistics include the number of cache hits (gets for keys that existed), the number of cache misses (gets for keys that did not exist), and the number of values in the cache.


import javax.cache.CacheStatistics;

        CacheStatistics stats = cache.getCacheStatistics();
        int hits = stats.getCacheHits();
        int misses = stats.getCacheMisses();
		

The App Engine implementation does not support resetting the hit and miss counts. Hit and miss counts are maintained indefinitely, but may be reset due to transient conditions of the memcache servers.

Monitoring memcache in the Google Cloud Platform Console

The app can retrieve statistics about its own use of the cache. These statistics are useful for monitoring and tuning cache behavior. You access statistics using a CacheStatistics object, which you get by calling the Cache is getCacheStatistics() method.

Available statistics include the number of cache hits (gets for keys that existed), the number of cache misses (gets for keys that did not exist), and the number of values in the cache.


import javax.cache.CacheStatistics;

        CacheStatistics stats = cache.getCacheStatistics();
        int hits = stats.getCacheHits();
        int misses = stats.getCacheMisses();
		

The App Engine implementation does not support resetting the hit and miss counts. Hit and miss counts are maintained indefinitely, but may be reset due to transient conditions of the memcache servers. '); ----------------------------- insert into maintable values(251, 2100001 ,'Monitoring memcache in the Google Cloud Platform Console',

Go to the Memcache page in the Google Cloud Platform Console.

GO TO THE MEMCACHE PAGE

Look at the following reports:

Memcache service level: Shows if your application is using the Shared or Dedicated service level. If you are an owner of the project, you can switch between the two. Learn more about the service levels.

Hit ratio: Shows a percentage and the raw number of memcache hits and misses.

Items in the cache.

Oldest item age: The age of the oldest cached item. Note that the age of an item is reset every time it is used, either read or written.

Total cache size.

You can take any of the following actions:

New key: Add a new key to the cache.

Find a key: Retrieve an existing key.

Flush cache: Remove all the key-value pairs from the cache.

(Dedicated memcache only) Look through the list of Hot keys.

Hot keys are keys that receive more than 100 queries per second (QPS) in the memcache.

This list includes up to 100 hot keys, sorted by highest QPS.

Read the article Best Practices for App Engine Memcache for tips on how to distribute load more evenly across the keyspace.

HelloWorld

Memcache Examples

This page provides code examples in Java for using the low-level Memcache API. Memcache is a high-performance, distributed memory object caching system that provides fast access to cached data. To learn more about memcache, read the Memcache Overview.

Synchronous usage

Low-level API example using the synchronous MemcacheService:

appengine-java8/memcache/src/main/java/com/example/appengine/memcache/MemcacheSyncCacheServlet.java


@SuppressWarnings("serial")
// With @WebServlet annotation the webapp/WEB-INF/web.xml is no longer required.
@WebServlet(name = "MemcacheSync", description = "Memcache: Synchronous",
    urlPatterns = "/memcache/sync")
public class MemcacheSyncCacheServlet extends HttpServlet {

  @Override
  public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException,
      ServletException {
    String path = req.getRequestURI();
    if (path.startsWith("/favicon.ico")) {
      return; // ignore the request for favicon.ico
    }

    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 + "");
  }
}

Asynchronous usage

Low-level API example using AsyncMemcacheService:

AsyncMemcacheService asyncCache = MemcacheServiceFactory.getAsyncMemcacheService();

asyncCache.setErrorHandler(ErrorHandlers.getConsistentLogAndContinue(Level.INFO));

memcache/package-summary

Interface

1 . AsyncMemcacheService -->An asynchronous version of MemcacheService.

2. BaseMemcacheService --> Methods that are common between MemcacheService and AsyncMemcacheService.

3 ConsistentErrorHandler :

A marker interface to indicate that all MemcacheServiceException exceptions should be handled by ErrorHandler.handleServiceError(MemcacheServiceException).

4. ErrorHandler Deprecated

Use ConsistentErrorHandler instead

5. IMemcacheServiceFactory

The factory by which users acquire a handle to the MemcacheService.

6. MemcacheService

The Java API for the App Engine Memcache service.

7. MemcacheService.IdentifiableValue

Encapsulates an Object that is returned by MemcacheService.getIdentifiable(java.lang.Object).

8. Stats

Statistics from the cache, available via MemcacheService.getStatistics()

Classes

1. ConsistentLogAndContinueErrorHandler : Similar to the deprecated LogAndContinueErrorHandler but consistently handles all back-end related errors.

2. ErrorHandlers

Static utility for getting built-in ErrorHandlers.

3.Expiration : Expiration specifications on MemcacheService.putAll(Map , Expiration) and MemcacheService.put(Object, Object, Expiration) operations.

4. IMemcacheServiceFactoryProvider :Factory provider for IMemcacheServiceFactory.

5. LogAndContinueErrorHandler Deprecated: Use ConsistentLogAndContinueErrorHandler instead

6. MemcacheSerialization : Static serialization helpers shared by MemcacheServiceImpl and LocalMemcacheService.

7. MemcacheSerialization.ValueAndFlags : Tuple of a serialized byte array value and associated flags to interpret that value.

8.MemcacheSerialization.ValueAndFlags :Tuple of a serialized byte array value and associated flags to interpret that value.

9. MemcacheService.CasValues : A holder for compare and set values.

10. MemcacheServiceFactory : The factory by which users acquire a handle to the MemcacheService.

11. StrictErrorHandler : A strict error handler, which will throw MemcacheServiceException or InvalidValueException for any service error condition.

Exception

1. InvalidValueException :Thrown when a cache entry has content, but it cannot be read.

2. MemcacheServiceException An exception for backend non-availability or similar error states which may occur, but are not necessarily indicative of a coding or usage error by the application.

Package com.google.appengine.api.memcache Description

Provides fast but unreliable data storage, also accessible via a JCache interface. Objects may be stored in the cache with an explicit expiration time, but may also be evicted before that expiration to make room for newer, more active entries.

The cache is accessed via a MemcacheService object, obtained from the MemcacheServiceFactory. It offers the cache as a map from key Object to value Object.

In the Development Server, the system property memcache.maxsize can be set to limit the available cache, taking values like

100M

(the default),

10K

, or

768

(bytes).

Because the cache offers best-effort data storage, by default most errors are treated as a cache miss. More explicit error handling can be installed via MemcacheService#setErrorHandler(ErrorHandler)

Link
Eng. Ravi Kumar Verma

Simple and dept learning technology is very important part in software development. Same story has been implemented at this site, so learn and implement the inovative things, Thanks

Ravi Kumar Verma