This project is read-only.

Asynchronous usage

Example usage of the memcached client in two different Asynchronous http handlers, where one reads from the memcached server and the other adds to the memcached server.
Notice that there are no sleeps, no waits no wasted resources.
In a real world scenario you would naturally have a class that would either go to the cache or database/file whatever if there was a cachemiss, this is just a very simple example on how you can use the memcached client.
    public abstract class CacheHandler : IHttpAsyncHandler
    {
        /// <summary>
        /// Memcached client implementation.
        /// </summary>
        protected static IMemcachedClient MemcachedClient = new MemcachedClient();

        /// <summary>
        /// Gets a value indicating whether another request can use the <see cref="T:System.Web.IHttpHandler"/> instance.
        /// </summary>
        /// <value></value>
        /// <returns>true if the <see cref="T:System.Web.IHttpHandler"/> instance is reusable; otherwise, false.</returns>
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see cref="T:System.Web.IHttpHandler"/> interface.
        /// </summary>
        /// <param name="context">An <see cref="T:System.Web.HttpContext"/> object that provides
        /// references to the intrinsic server objects (for example, Request, Response, 
        /// Session, and Server) used to service HTTP requests.</param>
        public void ProcessRequest(HttpContext context)
        {
            throw new InvalidOperationException();
        }

        /// <summary>
        /// Initiates an asynchronous call to the HTTP handler.
        /// </summary>
        /// <param name="context">An <see cref="T:System.Web.HttpContext"/> object that provides 
        /// references to intrinsic server objects (for example, Request, 
        /// Response, Session, and Server) used to service HTTP requests.</param>
        /// <param name="cb">The <see cref="T:System.AsyncCallback"/> to call when the 
        /// asynchronous method call is complete. If <paramref name="cb"/> is null, the delegate is not called.</param>
        /// <param name="extraData">Any extra data needed to process the request.</param>
        /// <returns>
        /// An <see cref="T:System.IAsyncResult"/> that contains information about the status of the process.
        /// </returns>
        public abstract IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData);

        /// <summary>
        /// Provides an asynchronous process End method when the process ends.
        /// </summary>
        /// <param name="result">An <see cref="T:System.IAsyncResult"/> that 
        /// contains information about the status of the process.</param>
        public abstract void EndProcessRequest(IAsyncResult result);
    }

    /// <summary>
    /// Dummy http handler to show how to use the memcached client asynchronously.
    /// </summary>
    public class CacheGetHandler : CacheHandler
    {
        /// <summary>
        /// Initiates an asynchronous call to the HTTP handler.
        /// </summary>
        /// <param name="context">An <see cref="T:System.Web.HttpContext"/> object that provides 
        /// references to intrinsic server objects (for example, Request,
        /// Response, Session, and Server) used to service HTTP requests.</param>
        /// <param name="cb">The <see cref="T:System.AsyncCallback"/> to call when the 
        /// asynchronous method call is complete. If <paramref name="cb"/> is null, the delegate is not called.</param>
        /// <param name="extraData">Any extra data needed to process the request.</param>
        /// <returns>
        /// An <see cref="T:System.IAsyncResult"/> that contains information about the status of the process.
        /// </returns>
        public override IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
        {
            string cacheKey = HttpContext.Current.Request.QueryString["key"] ?? "key";
            return MemcachedClient.BeginGet<string>(cacheKey, cb, extraData);
        }

        /// <summary>
        /// Provides an asynchronous process End method when the process ends.
        /// </summary>
        /// <param name="result">An <see cref="T:System.IAsyncResult"/> that contains 
        /// information about the status of the process.</param>
        public override void EndProcessRequest(IAsyncResult result)
        {
            ICacheItem<string> cacheItem = MemcachedClient.EndGet<string>(result);
            if (!string.IsNullOrEmpty(cacheItem.Value))
            {
                HttpContext.Current.Response.Write(cacheItem.Value);
            }
            else
            {
                HttpContext.Current.Response.Write("Cache was empty");
            }
        }
    }

    /// <summary>
    /// Dummy http handler to show how to use the memcached client asynchronously.
    /// </summary>
    public class CacheSetHandler : CacheHandler
    {
        /// <summary>
        /// Initiates an asynchronous call to the HTTP handler.
        /// </summary>
        /// <param name="context">An <see cref="T:System.Web.HttpContext"/> object that 
        /// provides references to intrinsic server objects (for example, Request, 
        /// Response, Session, and Server) used to service HTTP requests.</param>
        /// <param name="cb">The <see cref="T:System.AsyncCallback"/> to call when the 
        /// asynchronous method call is complete. If <paramref name="cb"/> is null, the delegate is not called.</param>
        /// <param name="extraData">Any extra data needed to process the request.</param>
        /// <returns>
        /// An <see cref="T:System.IAsyncResult"/> that contains information about the status of the process.
        /// </returns>
        public override IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
        {
            string cacheKey = HttpContext.Current.Request.QueryString["key"] ?? "key";
            string cacheValue = HttpContext.Current.Request.QueryString["value"] ?? "value";
            ICacheItem<string> cacheItem = new CacheItem<string>
            {
                CacheKey = cacheKey,
                Value = cacheValue
            };
            return MemcachedClient.BeginAdd(cacheItem, cb, extraData);

        }

        /// <summary>
        /// Provides an asynchronous process End method when the process ends.
        /// </summary>
        /// <param name="result">An <see cref="T:System.IAsyncResult"/> that 
        /// contains information about the status of the process.</param>
        public override void EndProcessRequest(IAsyncResult result)
        {
            try
            {
                bool success = MemcachedClient.EndAdd<string>(result);
                HttpContext.Current.Response.Write(string.Format("Cache addition succeded:{0}", success));
            }
            catch (Exception e)
            {
                HttpContext.Current.Response.Write(e);
            }
        }
    }

Last edited Sep 20, 2010 at 9:46 PM by bjornbouetsmith, version 6

Comments

No comments yet.