• Memcached 使用


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Memcached.ClientLibrary;
    using System.Text;
    using System.Collections;
    using System.Web.Caching;
    public partial class test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string[] relatedKeys = new string[1];
                relatedKeys[0] = "UID";
                LM.Memcached.Cache.Set("UID", 123);
                CacheDependency cd = new CacheDependency(null, relatedKeys);
                LM.Memcached.Cache.Set("abc",DateTime.Now , cd);

            }
            //Cache.Insert("ccc", "adsfasdf");
            //Response.Write(Cache.Get("ccc"));
        }

        public void memcached()
        {
            //分布Memcachedf服务IP 端口
            string[] servers = { "192.168.2.26:11211" };

            //初始化池
            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 mc = new Memcached.ClientLibrary.MemcachedClient();
            mc.EnableCompression = false;
            StringBuilder sb = new StringBuilder();
            //写入缓存
            sb.AppendLine("写入缓存测试:");
            sb.AppendLine("<br>_______________________________________<br>");
            if (mc.KeyExists("cache"))
            {
                sb.AppendLine("缓存cache已存在");
            }
            else
            {
                mc.Set("cache", "写入缓存时间:" + DateTime.Now.ToString());
                sb.AppendLine("缓存已成功写入到cache");
            }
            sb.AppendLine("<br>_______________________________________<br>");
            sb.AppendLine("读取缓存内容如下:<br>");
            sb.AppendLine(mc.Get("cache").ToString());

            //测试缓存过期
            sb.AppendLine("<br>_______________________________________<br>");
            if (mc.KeyExists("endCache"))
            {
                sb.AppendLine("缓存endCache已存在,过期时间为:" + mc.Get("endCache").ToString());
            }
            else
            {
                mc.Set("endCache", DateTime.Now.AddMinutes(1).ToString(), DateTime.Now.AddMinutes(1));
                sb.AppendLine("缓存已更新写入到endCache,写入时间:" + DateTime.Now.ToString() + " 过期时间:" + DateTime.Now.AddMinutes(1).ToString());
            }
            //分析缓存状态
            Hashtable ht = mc.Stats();
            sb.AppendLine("<br>_______________________________________<br>");
            sb.AppendLine("Memcached Stats:");
            sb.AppendLine("<br>_______________________________________<br>");
            foreach (DictionaryEntry de in ht)
            {
                Hashtable info = (Hashtable)de.Value;
                foreach (DictionaryEntry de2 in info)
                {
                    sb.AppendLine(de2.Key.ToString() + ": " + de2.Value.ToString() + "<br>");
                }
            }
            Response.Write(sb.ToString());
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            LM.Memcached.Cache.Set("UID", 111);
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            if (LM.Memcached.Cache.Get("abc") == null)
            {
                LM.Memcached.Cache.Set("abc", DateTime.Now);
            }
            else
            {
                Response.Write( LM.Memcached.Cache.Get("abc").ToString());
            }
        }
    }

  • 相关阅读:
    MSDN Visual系列:利用关联来过滤MOSS中的BDC数据
    SharePoint2007中开箱即用的权限(策略)级别
    SharePoint命令提示符
    使自定义的aspx页面仅用于部分SharePoint站点
    SharePoint 2010 中的站点模板(Site Templates)
    MSDN Visual系列:MOSS企业级搜索之三——创建和使用搜索范围
    推荐《Office SharePoint Server 2007案例实战开发》
    MSDN Visual系列:MOSS企业级搜索之四——创建内容源用于爬网业务数据
    通过主机标头实现多个SharePoint Web应用程序共用一个端口
    MSDN Visual系列:在MOSS中创建一个BDC实体
  • 原文地址:https://www.cnblogs.com/lovejunxia/p/3282542.html
Copyright © 2020-2023  润新知