using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Memcached.ClientLibrary;
using System.Collections; namespace WL.Web.Common
{
public class MemcacheX
{ private MemcachedClient client;
private static MemcacheX memcache; /// <summary>
/// 构造方法
/// </summary>
protected MemcacheX()
{
SockIOPool pool = SockIOPool.GetInstance();
string[] servers = { "127.0.0.1:11211" };
pool.SetServers(servers);
pool.MinConnections = 3;
pool.MaxConnections = 5;
pool.InitConnections = 3;
pool.SocketConnectTimeout = 5000;
pool.Initialize();
this.client = new MemcachedClient();
client.EnableCompression = false;
} public static MemcacheX Instance()
{
if (memcache == null)
{
memcache = new MemcacheX();
}
return memcache;
} /// <summary>
/// 判断是否包含某个键
/// </summary>
/// <param name="argKey">键值</param>
/// <returns></returns>
public bool ContainKey(string argKey)
{
return client.KeyExists(argKey);
} /// <summary>
/// 添加缓存数据
/// </summary>
/// <param name="argKey">键值</param>
/// <param name="argValue">存储值</param>
/// <returns></returns>
public bool Add(string argKey, object argValue)
{
return client.Add(argKey, argValue);
} /// <summary>
/// 添加缓存数据
/// </summary>
/// <param name="argKey">键值</param>
/// <param name="argValue">存储值</param>
/// <param name="argDateExpiration">过期时间</param>
/// <returns></returns>
public bool Add(string argKey, object argValue, DateTime argDateExpiration)
{
return client.Add(argKey, argValue, argDateExpiration);
} /// <summary>
/// 添加缓存数据
/// </summary>
/// <typeparam name="T">存储对象类型</typeparam>
/// <param name="argKey">键值</param>
/// <param name="entity">存储值</param>
/// <returns></returns>
public bool Add<T>(string argKey, T entity) where T : class
{
return client.Add(argKey, entity);
} /// <summary>
/// 添加缓存数据
/// </summary>
/// <typeparam name="T">存储对象类型</typeparam>
/// <param name="argKey">键值</param>
/// <param name="entity">存储值</param>
/// <param name="argDateExpiration">过期时间</param>
/// <returns></returns>
public bool Add<T>(string argKey, T entity, DateTime argDateExpiration) where T : class
{
return client.Add(argKey, entity, argDateExpiration);
} /// <summary>
/// 添加缓存数据,如果存在则替换原有数据
/// </summary>
/// <param name="argKey">键值</param>
/// <param name="argValue">存储值</param>
/// <returns></returns>
public bool Set(string argKey, object argValue)
{
if (ContainKey(argKey))
{
return false;
}
return client.Set(argKey, argValue);
} /// <summary>
/// 添加缓存数据,如果存在则替换原有数据
/// </summary>
/// <param name="argKey">键值</param>
/// <param name="argValue">存储值</param>
/// <param name="argDateExpiration">过期时间</param>
/// <returns></returns>
public bool Set(string argKey, object argValue, DateTime argDateExpiration)
{
if (ContainKey(argKey))
{
return false;
}
return client.Set(argKey, argValue, argDateExpiration);
} /// <summary>
/// 添加缓存数据,如果存在则替换原有数据
/// </summary>
/// <typeparam name="T">存储对象类型</typeparam>
/// <param name="argKey">键值</param>
/// <param name="entity">存储值</param>
/// <returns></returns>
public bool Set<T>(string argKey, T entity) where T : class
{
if (ContainKey(argKey))
{
return false;
}
return client.Set(argKey, entity);
} /// <summary>
/// 添加缓存数据,如果存在则替换原有数据
/// </summary>
/// <typeparam name="T">存储对象类型</typeparam>
/// <param name="argKey">键值</param>
/// <param name="entity">存储值</param>
/// <param name="argDateExpiration">过期时间</param>
/// <returns></returns>
public bool Set<T>(string argKey, T entity, DateTime argDateExpiration) where T : class
{
if (ContainKey(argKey))
{
return false;
}
return client.Set(argKey, entity, argDateExpiration);
} /// <summary>
/// 替换原有缓存
/// </summary>
/// <param name="argKey">键值</param>
/// <param name="argValue">存储值</param>
/// <returns></returns>
public bool Replace(string argKey, object argValue)
{
return client.Replace(argKey, argValue);
} /// <summary>
/// 替换原有缓存
/// </summary>
/// <param name="argKey">键值</param>
/// <param name="argValue">存储值</param>
/// <param name="argDateExpiration">过期时间</param>
/// <returns></returns>
public bool Replace(string argKey, object argValue, DateTime argDateExpiration)
{
return client.Replace(argKey, argValue, argDateExpiration);
} /// <summary>
/// 替换原有缓存
/// </summary>
/// <typeparam name="T">存储类型</typeparam>
/// <param name="argKey">键值</param>
/// <param name="entity">存储值</param>
/// <returns></returns>
public bool Replace<T>(string argKey, T entity) where T : class
{
return client.Replace(argKey, entity);
} /// <summary>
/// 替换原有缓存
/// </summary>
/// <typeparam name="T">存储类型</typeparam>
/// <param name="argKey">键值</param>
/// <param name="entity">存储值</param>
/// <param name="argDateExpiration">过期时间</param>
/// <returns></returns>
public bool Replace<T>(string argKey, T entity, DateTime argDateExpiration) where T : class
{
return client.Replace(argKey, entity, argDateExpiration);
} /// <summary>
/// 获得缓存数据
/// </summary>
/// <param name="argKey">键值</param>
/// <returns></returns>
public object Get(string argKey)
{
return client.Get(argKey);
} /// <summary>
/// 获得缓存数据
/// </summary>
/// <typeparam name="T">返回数据类型</typeparam>
/// <param name="argKey">键值</param>
/// <returns></returns>
public T Get<T>(string argKey)
{
T entity = default(T);
entity = (T)client.Get(argKey);
return entity;
} /// <summary>
/// 移除一个缓存数据
/// </summary>
/// <param name="argKey">键值</param>
/// <returns></returns>
public bool Remove(string argKey)
{
return client.Delete(argKey);
} /// <summary>
/// 移除一个缓存数据
/// </summary>
/// <param name="argKey">键值</param>
/// <param name="argDateExpiration">数据过期时间</param>
/// <returns></returns>
public bool Remove(string argKey, DateTime argDateExpiration)
{
return client.Delete(argKey, argDateExpiration);
} /// <summary>
/// 移除所有缓存数据
/// </summary>
/// <returns></returns>
public bool Remove()
{
return client.FlushAll();
} /// <summary>
/// 移除所有缓存数据
/// </summary>
/// <param name="servers">服务器地址</param>
/// <returns></returns>
public bool Remove(ArrayList servers)
{
return client.FlushAll(servers);
} }
}

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; using System.Data;
using yww.Utils;
using System.Collections;
using Memcached.ClientLibrary;
using System.Text;
namespace WL.Web
{
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{ }
} private void myload()
{
//分布Memcachedf服务IP 端口
string[] servers = { "192.168.1.83: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;
Hashtable ht = mc.Stats();
StringBuilder sb = new StringBuilder();
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());
} public void clear()
{
string[] servers = { "192.168.1.83: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;
mc.Delete("dt");
Response.Write("清空缓存成功");
} public void tt()
{
//分布Memcachedf服务IP 端口
string[] servers = { "192.168.1.83: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());
} public void setTable()
{ StringBuilder sb = new StringBuilder();
DataTable dt = Common.DataBase.GetTableRegion();
sb.AppendLine("---------------------------------<br>");
sb.AppendLine("DataTable 条数:"+dt.Rows.Count+"<br>");
Common.MemcacheX mc = Common.MemcacheX.Instance(); if (mc.ContainKey("dt"))
{
sb.AppendLine("缓存dt已存在");
}
else
{
bool b = mc.Set<DataTable>("dt", dt);
sb.AppendLine("DataTable保存到MC中返回结果:" + b + "<br>");
} sb.AppendLine("---------------------------------<br>"); Response.Write(sb.ToString());
} public void getTable()
{ StringBuilder sb = new StringBuilder(); sb.AppendLine("---------------------------------<br>"); Common.MemcacheX mc = Common.MemcacheX.Instance();
DataTable dt = mc.Get<DataTable>("dt");
if (dt != null)
{
sb.AppendLine("cache 中读取到"+dt.Rows.Count+"条记录<br>");
}
else
{
sb.AppendLine("cache 中没有读到数据<br>");
}
sb.AppendLine("---------------------------------<br>"); Response.Write(sb.ToString());
} protected void btn_save_Click(object sender, EventArgs e)
{
setTable();
} protected void btn_load_Click(object sender, EventArgs e)
{
getTable();
} protected void btn_clear_Click(object sender, EventArgs e)
{
clear();
}
protected void btn_stats_Click(object sender, EventArgs e)
{
myload();
} }
}

  https://files.cnblogs.com/files/ainidewen/资料.rar

Memcached Cache的更多相关文章

  1. MemCached Cache Java Client封装优化历程

    1.什么是memcached?(从官网翻译翻译) 免费和开源.高性能.分布式内存对象缓存系统,通用在自然界,但用于加速动态web应用程序,减轻数据库负载. Memcached是一个内存中的键值存储为小 ...

  2. [转载]memcached完全剖析--1. memcached的基础

    转载自:http://charlee.li/memcached-001.html 翻译一篇技术评论社的文章,是讲memcached的连载.fcicq同学说这个东西很有用,希望大家喜欢. 发表日:200 ...

  3. Memcached入门

    Memcached是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载. 它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态.数据库驱动网站的速度. Memcache ...

  4. memcached完全剖析–1. memcached的基础

    系列文章导航: memcached完全剖析–1. memcached的基础 memcached全面剖析–2. 理解memcached的内存存储 memcached全面剖析–3. memcached的删 ...

  5. memcached客户端的使用

    一. 概念 Memcached是danga.com(运营LiveJournal的技术团队)开发的一套分布式内存对象缓存系统,用于在动态系统中减少数据库负载,提升性能. 二. 适用场合 1. 分布式应用 ...

  6. memcached搭建缓存系统

    Memcached是danga.com(运营LiveJournal的技术团队)开发的一套分布式内存对象缓存系统,用于在动态系统中减少数据库负载,提升性能. 二.适用场合 1.分布式应用.由于memca ...

  7. memcached学习(4). memcached的分布式算法

    memcached的分布式 正如第1次中介绍的那样, memcached虽然称为"分布式"缓存服务器,但服务器端并没有"分布式"功能. 服务器端仅包括 第2次. ...

  8. Memcached 使用

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...

  9. memcached全面剖析–4. memcached的分布式算法

    memcached的分布式 正如第1次中介绍的那样, memcached虽然称为“分布式”缓存服务器,但服务器端并没有“分布式”功能. 服务器端仅包括 第2次. 第3次 前坂介绍的内存存储功能,其实现 ...

随机推荐

  1. linux 内核移植和根文件系统的制作【转载】

    原文地址:http://www.cnblogs.com/hnrainll/archive/2011/06/09/2076214.html 1.1 Linux内核基础知识 在动手进行Linux内核移植之 ...

  2. GMchess Linux下的中国象棋游戏

    gmchess,一款Linux下的中国象棋程序

  3. POJ1456:Supermarket(并查集版)

    浅谈并查集:https://www.cnblogs.com/AKMer/p/10360090.html 题目传送门:http://poj.org/problem?id=1456 堆作法:https:/ ...

  4. webrtc windows下的编译

    mkdir webrtc-checkoutcd webrtc-checkout set DEPOT_TOOLS_WIN_TOOLCHAIN=0set GYP_GENERATORS=ninjaset G ...

  5. Jetty + Servlet 实现文件下载

    Jetty非常适合做嵌入式web开发,正如Jetty的口号"Don’t deploy your application in Jetty, deploy Jetty in your appl ...

  6. (转)list_orderby

    本文转载自:http://blog.csdn.net/liyifei21/article/details/6558098 一个条件排序情况 list.OrderBy(item => tem.St ...

  7. 基于OpenCV的火焰检测(三)——HSI颜色判据

    上文向大家介绍了如何用最简单的RGB判据来初步提取火焰区域,现在我要给大家分享的是一种更加直观的判据--HSI判据. 为什么说HSI判据是更加直观的判据呢?老规矩,先介绍一下HSI色彩模型: HSI颜 ...

  8. wpf数据验证实例及常用方法小结

    虽然标题是wpf数据验证,但并不是对IDataErrorInfo.ValidationRule.属性中throw Exception这几种验证方式的介绍: 之前做项目时(例如员工工资管理),根据员工编 ...

  9. SQL基础(3)

    SQL FULL JOIN (1)SQL FULL JOIN关键字 只要其中某个表存在匹配,FULL JOIN 关键字就会返回行. (2)语法 SELECT column_name(s) FROM t ...

  10. IT求职经验分享

    本来这些东西应该早点写出来,之前由于一些事情的顾虑迟迟没有发表下笔整理并发表出来.最近及接下来的日子是校招找工作的关键时期,结合自己之前找暑期实习的过程及笔试面试经历(主要是三家,腾讯.360.绿盟科 ...