• ASP.NET下运用Memcached


    对于大型网站的高并发,在ASP.NET网站下的session性能并不高,所以造成人们一种印象,大型WEB项目使用JAVA的错觉,致使很多人吐槽微 软不给力,其实这好比拉不出怪地球引力,本文介绍Memcached在ASP.net Web项目中的应用,智联招聘,招商银行,农业银行等都是采用解决 方案,在性能上是绝对不亚于任何大型网站.同时Memcached还能很方便建立起服务器集群,对于大型解决方案,服务器集群的重要性不言而喻;
    1.准备工作.
    要在项目中使用到Memcached,需要准备好如下条件:
    服务器环境:安装Memcached服务到服务器上
    a.下载Memcached安装文件
    b.以管理员身份运行CMD 在下载的Memcached服务安装路径下安装Memcached服务(命令行:X:memcached.exe -d install)
    C.检查服务安装

    d.启动服务 命令行 memcached.exe –d start  当然可以直接在计算机服务管理来操作
    到这里Memcached服务就搭建完成了,那么如何运用到.NET项目中区呢?
    2.下载.NET Memcached lbr
    从文件..trunkclientlibsrcclientlibin2.0Debug下拷贝出4个DLL文件 Commons.dll,ICSharpCode.SharpZipLib.dll,log4net.dll,Memcached.ClientLibrary.dll, 添加引用到项目中去
    3.初始化Memcached
    因为时间关系快速建立一个项目,说明怎么使用,和一些使用场景,不做具体设计,
    这里我建立了一个Memcached帮助项目,下只有一个Memcached类,此类下初始化Memcached等操作已经提供存放和取得数据的两个方法

    C# code?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Memcached.ClientLibrary;
     
    namespace MemcachedHelper
    {
        public  class Memcached
        {
            public static MemcachedClient _Memcache = new MemcachedClient();
     
             
            //取值
            public static Object Get(String key)
            {
                //这里初始化服务器IP地址,可写在配置文件这里读取即可,注意这里是个字符串数组
                      这里如果是多个服务器(即服务器集群,只需依次在数组中讲服务器IP端口添加进去就完成了),在Memcached中服务器集群就这么实现了,个人觉得太牛逼了,至于远离由于时间关系有空在说吧
                string[] serverlist = { "127.0.0.1:11211" };
                //初始化池
                string poolName = "MemcacheIOPool";
                SockIOPool pool = SockIOPool.GetInstance(poolName);
                pool.SetServers(serverlist);
                pool.InitConnections = 1;
                pool.MinConnections = 1;
                pool.MaxConnections = 500;
                pool.SocketConnectTimeout = 1000;
                pool.SocketTimeout = 3000;
                pool.MaintenanceSleep = 30;
                pool.Failover = true;
                pool.Nagle = false;
                pool.Initialize();//容器初始化
                _Memcache.PoolName = poolName;
                _Memcache.EnableCompression = false;
               return _Memcache.Get(key);
                 
            }
           //存值
            public static bool Set(string key,Object value)
            {
                try
                {
                    //这里初始化服务器IP地址,可写在配置文件这里读取即可,注意这里是个字符串数组
                      这里如果是多个服务器(即服务器集群,只需依次在数组中讲服务器IP端口添加进去就完成了),在Memcached中服务器集群就这么实现了,个人觉得太牛逼了,至于远离由于时间关系有空在说吧
                    string[] serverlist = { "127.0.0.1:11211" };
                    //初始化池
                    string poolName = "MemcacheIOPool";
                    SockIOPool pool = SockIOPool.GetInstance(poolName);
                    pool.SetServers(serverlist);
                    pool.InitConnections = 1;
                    pool.MinConnections = 1;
                    pool.MaxConnections = 500;
                    pool.SocketConnectTimeout = 1000;
                    pool.SocketTimeout = 3000;
                    pool.MaintenanceSleep = 30;
                    pool.Failover = true;
                    pool.Nagle = false;
                    pool.Initialize();//容器初始化
                    _Memcache.PoolName = poolName;
                    _Memcache.EnableCompression = false;
                    _Memcache.EnableCompression = false;
                     _Memcache.Set(key, value,System.DateTime.Now.AddMinutes(20));
                     return true;
                }
                catch (Exception ex)
                {
                     
                    throw;
                }   
            }
        }
        [Serializable]
        public class MyClass
        {
            string a;
     
            public string A
            {
                get return a; }
                set { a = value; }
            }
            String b;
     
            public String B
            {
                get return b; }
                set { b = value; }
            }
        }
    }


    由于时间关系直接给出简单代码了,只为说明使用方式.
    客户端调用就很简单了 直接调用帮助类的相关方法:
               

    C# code?
    1
    2
    3
    4
    MyClass m=new MyClass();
                m.A="1";
                m.B="2";
                MemcachedHelper.Memcached.Set("Test1", m);


     

    C# code?
    1
    2
    3
     MyClass m1 = new MyClass();
                m1=MemcachedHelper.Memcached.Get("Test1"as MyClass;
                MessageBox.Show(m1.A.ToString()+m1.B.ToString());



    简单说几个应用场景吧:比如常见的登录,登录后要在服务端直接在Memcached中保存下登录用户信息,客户端以Coiked保存下Key值,这样就可 以不用session而达到一个用户登录状态保持的目的,这样的场景还有很多,当然我上面给的代码都是没经过优化的,一般来说像初始化连接池直接放到一个 控制器这些就请诸位各自斟酌了,至于Memcached的原理以后有空在详细说吧

  • 相关阅读:
    idea spring boot 1.x junit单元测试
    linux oracle/jdk启用大页面
    jdk8之CompletableFuture与CompletionService
    gc日志深入解析-覆盖CMS、并行GC、G1、ZGC、openj9
    h2 web console使用
    LockSupport工具类详解
    反射、Unsafe、直接调用性能大比拼
    spring boot druid动态多数据源监控集成
    Linux网络
    org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection总结
  • 原文地址:https://www.cnblogs.com/mooncher/p/4155252.html
Copyright © 2020-2023  润新知