Экспорт (0) Печать
Развернуть все

How to Use Co-located Caching

Обновлено: Январь 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

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 Windows Azure is available at Windows Azure Caching (Preview).

Prerequisites

To use co-located caching

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

  2. Create a Windows Azure project for your application, also as documented at Creating a Hello World Application for Windows 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 Windows Azure role: Expand your Windows Azure project, right-click WorkerRole1, click Windows 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: свойства

    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 Windows Azure toolbar, click the Run in Windows 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 Windows 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 Windows Azure dialog. Otherwise you may select a specific Azure storage account here. After you close the Caching properties dialog, within the Windows Azure toolbar, click the Publish to Windows Azure Cloud icon and follow the prompts to complete your deployment. After the application is running as a Windows Azure cloud service, launch the URL for your application. Refresh your browser, and you can see that values are maintained in the cache.

    noteПримечание
    The (auto) setting will have the desired effect only if you publish your deployment using the Eclipse plugin’s publish wizard. If instead you publish the .cspkg file manually using an external mechanism, such as the Windows Azure management portal, the deployment will not function properly.

См. также

Показ:
© 2014 Microsoft