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. controller返回js中文变成?解决方案

    在使用spring-mvc的mvc的时候既享受它带来的便捷,又头痛它的一些问题,比如经典的中文乱码问题.现在是用json作为客户端和服务端 的数据交换格式貌似很流行,但是在springmvc中有时候会 ...

  2. 释放Windows C盘空间 -- 虚拟内存和休眠文件

    本文由Suzzz原创,发布于 http://www.cnblogs.com/Suzzz/p/4111718.html ,转载请保留此声明. 项目组有一Windows工作站, 由于需要使用Kinect最 ...

  3. 备注信息的textarea 和 数据库 text类型

    有时候需要用到备注信息 备注一些东西 但是它又不同于普通的输入框,要有换行啊 空格之类的,更有甚者还有其他更多的需求 1.更多需求 用富文本编辑器 2.普通需求 直接用input type=" ...

  4. 配置进程外Session

    配置进程外Session: (1)将服务器Session信息存储在进程外           <1> 首先,开启asp.net state 服务: 控制面板 -> 程序和功能 -&g ...

  5. HDU5468(dfs序+容斥原理)

    Puzzled Elena Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  6. POJ 3624 Charm Bracelet(01背包模板)

    Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 45191   Accepted: 19318 ...

  7. 从文件中读取yuv和h264数据

    1.从文件中读取h264数据 参考ffmpeg avc.c写的从文件中一帧帧读取h.264数据的demo #include <stdio.h> #include <stdlib.h& ...

  8. C语言学习笔记--条件编译

    C语言中的条件编译的行为类似于 C 语言中的 if…else…,是预编译指示命令,用于控制是否编译某段代码 . 1.条件编译的本质 (1)预编译器根据条件编译指令有选择的删除代码 (2)编译器不知道代 ...

  9. executeUpdate,executeQuery,executeBatch 的区别

    executeQuery : 用于实现单个结果集,例如: Select 一般使用executeQuery 就是来实现单个结果集的工具 executeUpdate 用于执行 INSERT.UPDATE ...

  10. 错误信息:"OraOLEDB.Oracle" 返回了消息 "ORA-12154: TNS: 无法解析指定的连接标识符

    错误信息:"OraOLEDB.Oracle" 返回了消息 "ORA-12154: TNS: 无法解析指定的连接标识符 链接服务器"NC"的 OLE D ...