Hade du nytta av den här sidan?
Din feedback om det här innehållet är viktig. Berätta vad du tycker.
Ytterligare feedback?
1500 tecken kvar
EN
Det här innehållet finns inte tillgängligt på ditt språk men här finns den engelska versionen,

# How to Use Dedicated Caching

Updated: March 10, 2015

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

The application will look similar to the following:

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 co-located caching, see How to Use Co-located Caching.

## To use dedicated caching

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

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

<%@page import="java.net.InetSocketAddress"%>
<%@page import="net.spy.memcached.*"%>
<%@page import="java.util.concurrent.*"%>
<%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<body>
<%
// Retrieve the current time in milliseconds.
long timeMillis  = System.currentTimeMillis();
// Retrieve the role instance ID.
String currrentRoleInstanceID = System.getenv("RoleInstanceID");
// Create an instance of the memcached client.
MemcachedClient client = new MemcachedClient(new InetSocketAddress("localhost_WorkerRole1",11212));
// Create a string based on the current time and the current role instance ID.
String s = timeMillis + " - " + currrentRoleInstanceID;

String cachedString = null;
Future<Object> obj = null;
String keyName = "myText";

// Retrieve and display the currently cached value.
obj = client.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/>");

client.set(keyName, 3600, s);
// Retrieve and display the newly cached value.
obj = client.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>



Note that the call to the InetSocketAddress constructor (which is inside the cal to the MemcachedClient constructor) uses localhost_WorkerRole1. This assumes you will use WorkerRole1 for the name of the worker role that create for your Azure project in the next step.

5. Create an Azure project for your application, also as documented at Creating a Hello World Application for Azure in Eclipse. As previously mentioned, use WorkerRole1 as the name of the Azure role.

Note
Do not enable caching during the creation of the Azure project, as that will be handled when a dedicated caching role is created (in the next step).

6. Create an internal endpoint for the Azure deployment project: Within Eclipse, expand the Azure deployment project, right-click WorkerRole1, click Properties, expand Azure Role, and then click Endpoints.

8. Using the Add Endpoint dialog, add an internal endpoint named memcache_default with 11212 used as the private port.

9. Click OK to close the Add Endpoint dialog.

10. Add the caching library as a component to your caching worker role:

1. In the left hand properties pane, click Components.

2. In the Components dialog, click Add.

3. In the Add Component dialog, under Import, for From path, click Directory.

4. In the Browse For Folder dialog, navigate to C:\Program Files\Microsoft SDKs\Azure\.NET SDK\v2.1\ref\Caching\MemcacheShim and click OK.

5. For the Import method, use copy.

6. For As name, use Caching, which will be the default when you click the text box.

7. Set Deploy Method to None.

8. Click OK to close the Add Component dialog.

11. Add the caching shim files as a component to your caching worker role:

1. In the Components dialog, click Add.

2. In the Add Component dialog, under Import, for From path, click Directory.

3. In the Browse For Folder dialog, navigate to C:\Program Files\Microsoft SDKs\Azure\.NET SDK\v2.1\ref\Caching\MemcacheShim and click OK.

4. For the Import method, use copy.

5. For As name, use Caching\. (this will enable the shim files to be copied to the directory you used for the previous component).

6. Set Deploy Method to None.

7. Click OK to close the Add Component dialog.

12. Add an executable to start the caching shim as a component to your caching role:

1. In the Components dialog, click Add.

2. In the Add Component dialog, leave From path blank.

3. Set Import Method to None.

4. Set As name to MemcacheShimInstaller.exe.

5. Set Deploy Method to exec.

6. Set To directory to Caching.

7. Click OK to close the Add Component dialog, and then click OK to close the Components dialog.

13. Create a file named web.config within the approot of WorkerRole1. Use the following values for the contents of web.config.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="dataCacheClients"
type="Microsoft.ApplicationServer.Caching.DataCacheClientsSection, Microsoft.ApplicationServer.Caching.Core"
allowLocation="true"
allowDefinition="Everywhere"/>
</configSections>
<dataCacheClients>
<tracing sinkType="DiagnosticSink" traceLevel="Error"/>
<dataCacheClient name="DefaultShimConfig" useLegacyProtocol="false">
<autoDiscover isEnabled="true" identifier="DedicatedCache"/>
</dataCacheClient>
</dataCacheClients>
</configuration>

14. For the Azure project that you just created, add a new worker role to handle the dedicated caching. Within Eclipse, right-click the Azure deployment project, click Properties, expand Azure, and then click Roles.

16. In the Azure Role dialog:

1. Provide a name for the role. For this tutorial, use DedicatedCache.

2. In the left hand properties page, expand Azure Role and click Caching.

3. Within the Caching properties dialog:

1. Enable co-located caching. Although the user interface indicates the properties are for a co-located cache, since we will set the memory usage to 100% (in the next step), the role in effective will act as a dedicated cache, not a co-located cache.

2. Set the memory use to 100%.

3. Select (auto) from the drop-down list in the Storage section. This will cause the Eclipse toolkit to 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. (If you want to test only in the compute emulator, it doesn’t matter what value you select, but your application won’t work in a cloud deployment unless you select (auto) or a valid storage account name.)

Note
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.

4. Click OK to close the Caching properties dialog.

4. Click OK to close the Azure properties dialog.

17. 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.

18. To run in Azure, ensure that you have selected (auto) as the storage account name in the Caching property page for the DedicatedCache worker role. Then 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.