Skip to content
Ali Kheyrollahi edited this page Oct 14, 2013 · 5 revisions

CacheCow.Server

  1. Create an ASP.NET Web API project

  2. Use Visual Studio's Package Manager to download and add reference to CacheCow.Server NuGet package.

PM> Install-package CacheCow.Server

  1. Add CachingHandler as a message handler:

GlobalConfiguration.Configuration.MessageHandlers.Add(new CachingHandler());

CacheCow.Client

  1. Create a WPF, windows forms or Console project

  2. Use Visual Studio's Package Manager to download and add reference to CacheCow.Client NuGet package.

PM> Install-package CacheCow.Client

  1. Add CachingHandler as a message handler:

var client = new HttpClient(new CachingHandler() { InnerHandler = new HttpClientHandler() } );

CacheCow.Server.EntityTagStore.SqlServer

  1. In your ASP.NET Web API where you use CacheCow.Server, use NuGet to get the package

PM> Install-Package CacheCow.Server.EntityTagStore.SqlServer

  1. Browse to \packages\CacheCow.Server.EntityTagStore.SqlServer.0.1.0\scripts and use script.sql and run it against your database

  2. In your web.config define a connection string with name ConnectionName and set up parameters

  3. Pass an instance of SqlServerEntityTagStore when adding CachingHandler to message handlers:

    var connectionString = ConfigurationManager .ConnectionStrings["ConnectionName"].ConnectionString;

    var eTagStore = new SqlServerEntityTagStore(connectionString); var cacheHandler = new CachingHandler(eTagStore); config.MessageHandlers.Add(cacheHandler);

CacheCow.Server.EntityTagStore.Memcached

  1. Download Memcached v1.4 from here https://mega.co.nz/#!4JlAHY7K!H0f2h0iXQWdIBzEC_e2FJWVAXlAAdDhcXAGxONdjiCo

  2. Extract the Memcached 1.4.zip to C: or any other destination

  3. Run the following command using CMD memcached.exe -p 21201 -r -U 21202 -c 200 -n 65536 -l localhost -m 128

  • -p 21201 TCP port number to listen on (default: 11211)
  • -r maximize core file limit
  • -U 21202 UDP port number to listen on (default: 11211, 0 is off)
  • -c 200 max simultaneous connections (default: 1024)
  • -n 65536 minimum space allocated for key+value+flags (default: 48)
  • -l localhost interface to listen on (default: INADDR_ANY, all addresses)
  • -m 128 max memory to use for items in megabytes (default: 64 MB)
  1. In your ASP.NET Web API where you use CacheCow.Server, use NuGet to get the package

PM> Install-Package CacheCow.Server.EntityTagStore.Memcached

  1. Modify your web.config by adding a new sectionGroup

Now add the following just before the end of the web.config

<enyim.com>
<memcached protocol="Binary">
  <servers>
<add address="127.0.0.1" port="21201" />
  </servers>
</memcached>
</enyim.com>
  1. The last part is to modify your WebApiConfig.cs.

    var eTagStore = new MemcachedEntityTagStore(ConfigurationManager .GetSection("enyim.com/memcached") as MemcachedClientSection); var cacheHandler = new CachingHandler(eTagStore);
    config.MessageHandlers.Add(cacheHandler);

To test it do a request on some endpoint of your API and look at the headers.

ETag →W/"4b6eac845d3249e380554c30b6e9d2bc"

Now you can do a

If-None-Match "4b6eac845d3249e380554c30b6e9d2bc"

and you'll get 304 Not Modified

Indicates the resource has not been modified since last requested. Typically, the HTTP client provides a header like the If-Modified-Since header to provide a time against which to compare. Using this saves bandwidth and reprocessing on both the server and client, as only the header data must be sent and received in comparison to the entirety of the page being re-processed by the server, then sent again using more bandwidth of the server and client.

Additional

You can use telnet to access your Memcached server.

If telnet is not a recognized command do the following:

Go to Control Panel then to Programs and then click on Turn Windows features on or off. Click the checkbox next to Telnet client then click OK.

Open CMD and type the following

telnet localhost 21201
stats 

after which you'll see

STAT pid 6888
STAT uptime 23
STAT time 1381735664
STAT version 1.4.5_4_gaa7839e
STAT pointer_size 32
STAT curr_connections 10
STAT total_connections 11
STAT connection_structures 11
STAT cmd_get 0
STAT cmd_set 0
STAT cmd_flush 0
STAT get_hits 0
STAT get_misses 0

or type

stats items 

to get items statistics (count, age, ...)

STAT items:1:number 2
STAT items:1:age 459
STAT items:1:evicted 0
STAT items:1:evicted_nonzero 0
STAT items:1:evicted_time 0
STAT items:1:outofmemory 0
STAT items:1:tailrepairs 0
STAT items:1:reclaimed 0