Use a cache

Source: Internet
Author: User

To create high-performance systems, sometimes-need to cache data. Play has a cache library and would use Memcached when used in a distributed environment.

If you don ' t configure Memcached, Play would use a standalone cache, which stores data in the JVM heap. Caching data in the JVM application breaks the "share nothing" assumption made by Play:you can ' t run your application on Several servers, and expect the application to behave consistently. Each application instance would have a different copy of the data.

It's important to understand that the cache contract are Clear:when you put data in a cache and you can ' t expect that data t O remain there forever. In fact you shouldn ' t. A cache is fast, but values expire, and the cache generally exists only in memory (without persistent backup).

The best-of-the-repopulate it when it doesn ' t has what you expect:

public static void allProducts() {    List<Product> products = Cache.get("products", List.class);    if(products == null) {        products = Product.findAll();        Cache.set("products", products, "30mn");    }    render(products);}
The Cache API

The cache API is provided by the Play.cache.Cache class. This class contains the set of methods to set, replace, and get data from the cache. Refer to the Memcached documentation to understand the exact behavior of each method.

Some Examples:

  public static void Showproduct (String ID) {Product product = Cache.get ("product_" + ID, product.class);        if (product = null) {Product = Product.findbyid (ID);    Cache.set ("product_" + ID, product, "30mn"); } render (product);}    public static void Addproduct (String name, int price) {Product Product = new Product (name, price);    Product.save (); Showproduct (ID);}    public static void Editproduct (string id, string name, int price) {Product Product = Product.findbyid (ID);    Product.name = name;    Product.price = Price;    Cache.set ("product_" + ID, product, "30mn"); Showproduct (ID);}    public static void Deleteproduct (String ID) {Product Product = Product.findbyid (ID);    Product.delete ();    Cache.delete ("product_" + ID); Allproducts ();}  

Some methods start with the safe prefix–e.g. safedelete, safeset. The standard methods is non-blocking. That's means that's when you issue the call:

Cache.delete("product_" + id);

The delete method would return immediately and would not wait for until the cached object is actually deleted. So if an error occurs–e.g. A IO error–the object may still be present.

When you need to make sure that the object was deleted before continuing, you can use the safedeletemethod:

Cache.safeDelete("product_" + id);

This method is blocking and returns a Boolean value indicating whether the object have been deleted or not. The full pattern of the ensures an item are deleted from the cache are:

if(!Cache.safeDelete("product_" + id)) {    throw new Exception("Oops, the product has not been removed from the cache");}...

Note that those being blocking calls, safe methods would slow down your application. So use them only when needed.

Don ' t use the Session as a cache!

If you come from the a framework that uses an in-memory Session implementation, you may be frustrated to see that Play allows Only a small set of String data to being saved in the HTTP Session. But this is much better because a session was not the place to the cache your application data!

So if you had been accustomed to doing things similar to:

httpServletRequest.getSession().put("userProducts", products);...// and then in subsequent requestsproducts = (List<Product>)httpServletRequest.getSession().get("userProducts");

On Play you achieve the same effect a little differently. We think it ' s a better approach:

Cache.set(session.getId(), products);...// and then in subsequent requestsList<Product> products = Cache.get(session.getId(), List.class)

Here we had used a unique UUID to keep unique information in the Cache for each user. Remember that, unlike a session object, the cache was not bound to any particular user!

Configure memcached

When you want to enable a real Memcached implementation, enable Memcached and define the daemon address in your applic ation.conf:

memcached=enabledmemcached.host=127.0.0.1:11211

You can connect to a distributed cache by specifying multiple daemon addresses:

memcached=enabledmemcached.1.host=127.0.0.1:11211memcached.2.host=127.0.0.1:11212

Continuing the discussion

Learn about sending emails.

Use a cache

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.