• 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);
        }
    }
  • 相关阅读:
    Django 请求生命周期
    Django views.py中的 FBV(函数) CBV(类)
    cookie session
    Django admin
    Django ORM QuerySet集合对象的特性
    Django ORM 多对多操作 使用聚合函数和分组 F查询与Q查询
    Django ORM 一对多操作
    Django ORM 单表操作
    Django 模板 template
    css之background-clip: text
  • 原文地址:https://www.cnblogs.com/shuai7boy/p/5318930.html
Copyright © 2020-2023  润新知