zoukankan      html  css  js  c++  java
  • WINDOWS SERVER 2008下面使用Memcached笔记

    Memcached这个产品还是很不错的,原来一直在linux下运行,LINUX的服务的设计思想,在性能上也是很不多,很多大型的网站都有用到它,而且现在有些数据库都会和它整合。

    我在做聚聚呀时,社区的FEED性能不高,对数据的运算要求比较多,CPU有时比较高,于是我就采用了Memcached来得提升下它的性能。

    我使用的环境是

    windows 2008 server 32位系统

    使用的版本是memcached-1.2.6-win32-bin,这个大家可以去他的官方网站上下载下载地址:http://code.jellycan.com/memcached/
    我先把我的使用步骤记下来

    1,安装Memcached

    为了方便使用,我直接把Memcached安装成WINDOWS SERVICE

    D:\MemCache>sc create memcached binPath= "e:\app\memcached-1.2.6-win32-bin\memcached.exe -p 9999 -l 192.168.1.10  -d runservice" DisplayName= "memcached_server" start= auto

    memcached的基本设置:

        -p 监听的端口
        -l 连接的IP地址, 默认是本机
        -d start 启动memcached服务
        -d restart 重起memcached服务
        -d stop|shutdown 关闭正在运行的memcached服务
        -d install 安装memcached服务
        -d uninstall 卸载memcached服务
        -u 以的身份运行 (仅在以root运行的时候有效)
        -m 最大内存使用,单位MB。默认64MB
        -M 内存耗尽时返回错误,而不是删除项
        -c 最大同时连接数,默认是1024
        -f 块大小增长因子,默认是1.25
        -n 最小分配空间,key+value+flags默认是48
        -h 显示帮助

    执行完这个命令后,我们在192.168.1.10  这个机器上可以看到多了一个windows 服务,它是memcached_server,它运行起来了就可以工作了,接下来是使用客户端来完成与它的交互,它是基于socket的网络应用,所以要注意它的端口,上面我们设置的端口是-p 9999,9999就是它的端口了

    .NET有一个memcache的客户端,我这里介绍一个用的比较好的客户端包是BeITMemcached_source_2009_02_27,大家可以下载来直接引用它里面的DLL 到工程项目中来

    好了我们现在开始试下,新建一个WEB APP,然后需要引用MMC客户端包BeITMemcached

    修改配置WEB.CONFIG

        <add key="CacheServer" value="192.168.1.10:9999"/><!--mmc server的服务器地址和端口-->
        <add key="CacheMaxPoolSize" value="5"/>
        <add key="TimeOut" value="5000"/>

    我这里是我自己封装了一个类库用于访问Memcache server

    测试代码如下

      protected void Page_Load(object sender, EventArgs e)
            {
                List<string> lst = new List<string>();
          
                for (int i = 0; i <= 50000; i++)
                {
                    lst.Add("fdfasfdsfasfasfdsafa"+i.ToString());
                }
                SNET.Common.MemCached.SNETMemCachedClient.Set("mylst", lst);
                List<string> flsg = SNET.Common.MemCached.SNETMemCachedClient.Get("mylst") as List<string>;
                foreach (string strs in flsg)
                {
                    Response.Write(strs + "<br>");
                }
                SNET.Common.MemCached.SNETMemCachedClient.Set("mystring", "The quick brown fox jumped over the lazy dog.");
                SNET.Common.MemCached.SNETMemCachedClient.Set("myarray", new string[] { "This is the first string.", "This is the second string." });
                SNET.Common.MemCached.SNETMemCachedClient.Set("myinteger", 4711);
                SNET.Common.MemCached.SNETMemCachedClient.Set("mydate", new DateTime(2008, 02, 23));
                //Use custom hash
                SNET.Common.MemCached.SNETMemCachedClient.Set("secondstring", "Flygande 测试啊~~~~....$$%%^^^$$#$$$", 4711);

                //Get a string
                string str = SNET.Common.MemCached.SNETMemCachedClient.Get("1_mystring") as string;
                str = SNET.Common.MemCached.SNETMemCachedClient.Get("mystring") as string;
                if (str != null)
                {
                    Response.Write("Fetched item with key: mystring, value: " + str + "<br>");
                }

                //Get an object
                string[] array = SNET.Common.MemCached.SNETMemCachedClient.Get("myarray") as string[];
                if (array != null)
                {
                    Response.Write("Fetched items with key: myarray, value 1: " + array[0] + ", value 2: " + array[1]+"<br>");
                }

                //Get several values at once
                object[] result = SNET.Common.MemCached.SNETMemCachedClient.Get(new string[] { "myinteger", "mydate" });
                if (result[0] != null && result[0] is int)
                {
                    Response.Write("Fetched item with key: myinteger, value: " + (int)result[0] + "<br>");
                }
                if (result[1] != null && result[1] is DateTime)
                {
                    Response.Write("Fetched item with key: mydate, value: " + (DateTime)result[1] + "<br>");
                }

                str = SNET.Common.MemCached.SNETMemCachedClient.Get("secondstring", 4711) as string;
                if (str != null)
                {
                    Response.Write("Fetched item with key and custom hash: secondstring, value: " + str + "<br>");
                }
                SNET.Common.MemCached.SNETMemCachedClient.Replace("mystring", "替换的....");
                str = SNET.Common.MemCached.SNETMemCachedClient.Get("mystring") as string;
                if (str != null)
                {
                    Response.Write("Fetched item with key: mystring, value: " + str + "<br>");
                }
            }

    本地测试的性能还不错的。

    注意在大型的网站应用时,一定要设置合适的CacheMaxPoolSize,不然会假死,这个表示最大的PoolSize.

    下载测试包 

  • 相关阅读:
    python简单的游戏场景代码
    ZooKeeper应用场景
    Zookeeper配置文件中的配置项解释和Zookeeper的安装
    MapReduce的手机流量统计的案例
    java和js获取当前天之后或之前7天(任意)日期
    JDK中ThreadDump诊断Java代码中的线程死锁问题
    Java代码调用Oracle的存储过程,存储函数和包
    Oracle的卸载过程步骤
    JDK中ConcurrentHashMap效率测试
    JDK提供的四种线程池代码详解
  • 原文地址:https://www.cnblogs.com/Leung/p/1590091.html
Copyright © 2011-2022 走看看