代码改变世界
[登录 · 注册]
  • Memcached——非关系型数据库分布式处理
  • Memcached登录校验应用:

     

    MMCacheWriter.cs类

    using Memcached.ClientLibrary;
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Common
    {
        public class MMCacheWriter : ICacheWriter
        {
            public static readonly MemcachedClient MemcachedClient;
            static MMCacheWriter()
            {
                //分布Memcachedf服务IP 端口
                string[] servers = ConfigurationManager.AppSettings["memcachedServer"].Split(',');
                if (servers==null)
                {
                    throw new Exception("请选择正确的配置");
                }
                //初始化池
                SockIOPool pool = SockIOPool.GetInstance();
                pool.SetServers(servers);
                pool.InitConnections = 3;
                pool.MinConnections = 3;
                pool.MaxConnections = 5;
                pool.SocketConnectTimeout = 1000;
                pool.SocketTimeout = 3000;
                pool.MaintenanceSleep = 30;
                pool.Failover = true;
                pool.Nagle = false;
                pool.Initialize();
                //客户端实例
                MemcachedClient = new Memcached.ClientLibrary.MemcachedClient();
                MemcachedClient.EnableCompression = false;
               
            }
            public void Set(string key, object value, DateTime exp)
            {
                MemcachedClient.Set(key, value, exp);
            }
    
            public void Set(string key, object value)
            {
                MemcachedClient.Set(key, value);
            }
    
            public object Get(string key)
            {
               return MemcachedClient.Get(key);
            }
        }
    }

     CacheHelper.cs类

    using Spring.Context;
    using Spring.Context.Support;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Common
    {
        public  class CacheHelper
        {
            public static ICacheWriter CacheWriter { get; set; }
            /// <summary>
            /// 解决实例化对象问题
            /// </summary>
            static CacheHelper()
            {           
                IApplicationContext ctx = ContextRegistry.GetContext();
                var userInfoDal = ctx.GetObject("CacheHelper") as CacheHelper;
            }
           public static void Set(string key, object value, DateTime exp)
            {           
                CacheWriter.Set(key, value, exp);
            }
           public static void Set(string key, object value)
            {
                CacheWriter.Set(key, value);
            }
           public static object Get(string key)
            {
                return CacheWriter.Get(key);
            }
        }
    }

    HttpRunTimeCacheWriter.cs类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Web;
    
    namespace Common
    {
        public class HttpRunTimeCacheWriter : ICacheWriter
        {
    
            void ICacheWriter.Set(string key, object value, DateTime exp)
            {
                HttpRuntime.Cache.Insert(key, value, null, exp, TimeSpan.Zero);
            }
    
            void ICacheWriter.Set(string key, object value)
            {
                HttpRuntime.Cache.Insert(key, value);
            }
    
            object ICacheWriter.Get(string key)
            {
               return HttpRuntime.Cache[key];
            }
        }
    }

    ICacheWriter.cs接口

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Common
    {
        public interface ICacheWriter
        {
            void Set(string key, object value, DateTime exp);
            void Set(string key, object value);
            object Get(string key);
        }
    }
  • 【推广】 阿里云小站-上云优惠聚集地(新老客户同享)更有每天限时秒杀!
    【推广】 云服务器低至0.95折 1核2G ECS云服务器8.1元/月
    【推广】 阿里云老用户升级四重礼遇享6.5折限时折扣!
  • 原文:https://www.cnblogs.com/shuai7boy/p/5318930.html
Copyright 2008-2020 晋ICP备12007731号-1