zoukankan      html  css  js  c++  java
  • 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);
        }
    }
  • 相关阅读:
    linux下压力测试工具ab的使用
    linux下nginx日常操作
    centos7编译安装nginx
    ssh登录locale报错:cannot change locale (zh_CN.UTF-8): No such file or directory
    linux重装rabbitmq的问题
    rabbitmq3.6.5镜像集群搭建以及haproxy负载均衡
    linux安装rabbitmq3.6.5
    centos6升级glibc-2.14没有报错,但是验证没有升级成功的问题解决
    linux卸载erlang
    asp web服务
  • 原文地址:https://www.cnblogs.com/shuai7boy/p/5318930.html
Copyright © 2011-2022 走看看