My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
Usage  
How to use the JsMemcached client api.
Featured
Updated Jan 21, 2009 by rakesh...@gmail.com

Setup

  • Make sure you have Jaxer installed.
  • Put the JsMemcached.js file in some location. Ideally, this should not be inside your web-root, or it should be inside the jaxer-service (or similar) folder, so that it is not accessible to the outside world.
  • Create an HTML file somewhere in your document root.
  • Add the following script tag to it:
  • <script type="text/javascript" runat="server" src="path/to/JsMemcached.js"></script>
  • Ensure, of course, that a memcached server is available over the network (or locally).

Usage

Adding the script tag to the page makes available a global singleton object called JsMemcached. Use this object as your starting point for all memcache communication. This object internally uses Jaxer.Socket to communicate directly with the memcached server.

API

Note: Performance optimization has been a huge pita, because of the performance of the Jaxer runtime (or because of my inability to write better code). To keep performance as optimal as possible, I've had to remove almost all of the validation code. So, please ensure that you validate your parameters to the JsMemcached methods before you send it.

Configuration

In most cases, you'll be using JsMemcached on the same server as your application and on port 11211. If that's the case, you don't need to do anything - the default configuration in JsMemcached will do. However, if you want to change the settings, this is how you do it:

JsMemcached.config("cache.mydomain.com", 12345);

You could also use an explicit IP address instead of cache.mydomain.com above.

The storage commands: .add, .set and .replace

All these commands will save a value associated with the key in the memcached server.

Example:

    JsMemcached.add(key, value, time);
  • "set" means "store this data".
  • "add" means "store this data, but only if the server doesn't already hold data for this key".
  • "replace" means "store this data, but only if the server does already hold data for this key".

If the conditions above are not met, JsMemcached throws an error which will contain what the server said when performing the save operation.

Parameter Type Description
key String The key to identify the data with
value String The value you want to store
time String or Number The time for which you want to store data. Once this time has passed, the data will expire from memcache. The time has to be sent as the number of seconds since 1 Jan 1970, or as the number of milliseconds from the current time.

Returns: Nothing.

The retrieval command: .get

This command retrieves data from the memcached server.

Example:

    JsMemcached.get(key);

The key parameter should match something stored before in the memcached server.

Returns: The value corresponding to the key in the memcached server. If no match is found, an empty string is returned.

Note: If there's anyone who can take a look at the source, and find a way to optimize the string processing in this get call, and make it bullet-proof, please let me know. Though this call is already faster than a corresponding DB call, it's still about thrice as slow as other memcached clients. I would guess part of this problem is because of the runtime (Tamarin, where are you!), but any help will undoubtedly be great.

Wait, that's it?

For now, yes. I will be working on making this API complete with the memcached protocol specs, and intend to roll it out soon. Meanwhile, in a parallel thought, I'm worried about the performance of this thing, and want to make sure I'm using the processor juice as optimally as possible. I could do with some help to make this thing faster. Is there anybody out there?


Sign in to add a comment
Powered by Google Project Hosting