SALES: 1-800-867-1380

How to Use Co-located Caching

Updated: September 26, 2014

The following steps show you how to build an application that uses co-located memcached-compatible caching.

The application will look similar to the following:

Caching Hello World example

There are two main deployment topologies for caching: co-located and dedicated. Co-located roles also host other non-caching application code and services. Dedicated roles are worker roles that are used only for caching. For information about building an application that uses dedicated caching, see How to Use Dedicated Caching.

Additional information about caching in Azure is available at Azure Cache.

  1. Create a dynamic web application as documented at Creating a Hello World Application for Azure in Eclipse.

  2. Create a Azure project for your application, also as documented at Creating a Hello World Application for Azure in Eclipse.

  3. Add your Memcache client JAR to your Java build path as an external JAR library.

  4. Add your Memcache client JAR to your deployment assembly.

  5. Enable co-located caching by opening the properties for the Azure role: Expand your Azure project, right-click WorkerRole1, click Azure, and then click Caching. In the Caching properties dialog, click Enable co-located caching. Use the default values, or see Caching properties if you’d like to use other values. The following shows an example set of values:

    Caching properties

    Click OK to save your settings and close the Caching properties dialog.

  6. Modify index.jsp to use the following code:

    <%@ page import="java.net.*" %>
    <%@ page import="java.util.concurrent.*" %>
    <%@ page import="net.spy.memcached.*" %>
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    <%
        // Retrieve the current time in milliseconds.
        long timeMillis = System.currentTimeMillis();
        // Retrieve the role instance ID.
        String currentRoleInstanceID = System.getenv("RoleInstanceID");
        // Create an instance of the memcached client.
        MemcachedClient c = new MemcachedClient(new InetSocketAddress("localhost_WorkerRole1", 11211));
        // Create a string based on the current time and the current role instance ID. 
        String s = timeMillis + " - " + currentRoleInstanceID;
    
        String cachedString = null;
        Future<Object> obj = null;
        String keyName = "myText";
    
        // Retrieve and display the currently cached value.
        obj = c.asyncGet(keyName);
        try
        {
        cachedString = (String)obj.get(5, TimeUnit.SECONDS);
        }
        catch (TimeoutException e)
        {
        obj.cancel(false);
        // Take other action as needed.
        }
        
        out.println("Getting from cache: myText = " + cachedString + "<p/>");
    
        // Set the cached value to the current time and role instance ID. -->
        out.println("Setting to cache: myText = " + s + "<p/>");
        
        c.set(keyName, 3600, s);
        // Retrieve and display the newly cached value.
        obj = c.asyncGet(keyName);
        try
        {
            cachedString = (String)obj.get(5, TimeUnit.SECONDS);
        }
        catch (TimeoutException e)
        {
            obj.cancel(false);
        }
        out.println("Getting from updated cache: myText = " + cachedString + "<p/>");
    %>
    </body>
    </html>
    
  7. To run in the compute emulator, in the Azure toolbar, click the Run in Azure Emulator icon. After the application is running in the compute emulator, launch the localhost URL for your application. Refresh your browser, and you can see that values are maintained in the cache.

  8. To run in Azure, first open the Caching properties dialog and select (auto) from the drop-down list in the Storage section. If in Step 5 you set the storage account name to (auto) (which is the default), your caching configuration will automatically use the same storage account as the one selected by you in the Publish to Azure dialog. Otherwise you may select a specific Azure storage account here. After you close the Caching properties dialog, within the Azure toolbar, click the Publish to Azure Cloud icon and follow the prompts to complete your deployment. After the application is running as an Azure cloud service, launch the URL for your application. Refresh your browser, and you can see that values are maintained in the cache.

    noteNote
    The (auto) setting will have the desired effect only if you publish your deployment using the Eclipse toolkit’s publish wizard. If instead you publish the .cspkg file manually using an external mechanism, such as the Azure management portal, the deployment will not function properly.

See Also

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft