Syncronous usage

The following classes show how you can use the memcached client in a syncronous way. I do not recommend it.
As you can clearly see from the usage you have one thread blocking after each begin call, in comparison to the classes on the Asynchronous usage page, where no threads are blocking.
/// <summary>
    /// Abstract implementation of the IHttpHandler interface with a common memcached member.
    /// </summary>
    public abstract class CacheHandler : IHttpHandler
    {
        /// <summary>
        /// Memcached client implementation.
        /// </summary>
        protected static IMemcachedClient MemcachedClient = new MemcachedClient();

        /// <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 abstract void ProcessRequest(HttpContext context);

        /// <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>
    /// Dummy http handler to show how to use the memcached client asynchronously.
    /// </summary>
    public class CacheGetHandler : CacheHandler
    {
        /// <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 override void ProcessRequest(HttpContext context)
        {
            string cacheKey = HttpContext.Current.Request.QueryString["key"] ?? "key";
            ManualResetEvent evt = new ManualResetEvent(false);
            
            MemcachedClient.BeginGet<string>(
                cacheKey,
                ar =>
                {
                    ManualResetEvent evtInner = (ManualResetEvent)ar.AsyncState;
                    ICacheItem<string> cacheItem = MemcachedClient.EndGet<string>(ar);
                    HttpContext.Current.Response.Write(cacheItem.Value);
                    evtInner.Set();
                }, 
                evt);

            evt.WaitOne();
        }
    }

    /// <summary>
    /// Dummy http handler to show how to use the memcached client asynchronously.
    /// </summary>
    public class CacheSetHandler : CacheHandler
    {
        /// <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 override void ProcessRequest(HttpContext context)
        {
            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
            };

            ManualResetEvent evt = new ManualResetEvent(false);
            MemcachedClient.BeginAdd<string>(
                cacheItem,
                ar =>
                {
                    ManualResetEvent evtInner = (ManualResetEvent)ar.AsyncState;
                    bool success = MemcachedClient.EndAdd<string>(ar);
                    HttpContext.Current.Response.Write(string.Format("Cache addition succeded:{0}", success));
                    evtInner.Set();
                },
                evt);

            evt.WaitOne();
        }
    }

Last edited Sep 20, 2010 at 8:55 PM by bjornbouetsmith, version 1

Comments

No comments yet.