架构师_程序员_码农网

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

搜索
查看: 23962|回复: 0

[资料] c# memcached缓存测试demo

[复制链接]
发表于 2016-7-15 13:04:32 | 显示全部楼层 |阅读模式
QQ截图20160715130009.jpg

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Memcached.ClientLibrary;
  6. using System.Threading;

  7. namespace ConsoleApplication1
  8. {
  9.     [Serializable]
  10.     public class Student
  11.     {
  12.         public string id { get; set; }
  13.         public string name { get; set; }
  14.             
  15.     }
  16.     class Program
  17.     {
  18.         [STAThread]
  19.         static void Main(string[] args)
  20.         {
  21.             //参数设置
  22.             string SockIOPoolName = "itsvse_memchached";
  23.             string[] MemcacheServiceList = { "10.0.0.215:1998" };

  24.             //设置连接池
  25.             SockIOPool SPool = SockIOPool.GetInstance(SockIOPoolName);
  26.             SPool.SetServers(MemcacheServiceList);
  27.             SPool.Initialize();

  28.             //实例化Client
  29.             MemcachedClient MClient = new MemcachedClient();
  30.             MClient.PoolName = SockIOPoolName;

  31.             Console.WriteLine("1.创建memcache缓存Hello World");
  32.             MClient.Add("Key1001", "Hello World");
  33.             Console.WriteLine("2.查询缓存信息{0}", MClient.Get("Key1001"));

  34.             Console.WriteLine("3.修改memcache缓存Hello World");
  35.             MClient.Set("Key1001", "Hello World - 修改版");
  36.             Console.WriteLine("4.查询缓存信息{0}", MClient.Get("Key1001"));


  37.             if (MClient.KeyExists("Key1001"))
  38.             {
  39.                 Console.WriteLine("5.删除memcache缓存");
  40.                 MClient.Delete("Key1001");
  41.             }

  42.             if (MClient.KeyExists("Key1001"))
  43.                 Console.WriteLine(MClient.Get("Key1001"));
  44.             else
  45.                 Console.WriteLine("6.删除已删除");

  46.             Student stud = new Student() { id = "10001", name = "张三" };
  47.             MClient.Add("student", stud,DateTime.Now.AddMinutes(1));
  48.             Student Get_stud = MClient.Get("student") as Student;
  49.             Console.WriteLine("6.缓存实体对象:{0} {1}", Get_stud.id, Get_stud.name);

  50.             MClient.Add("Key1002", "我已设置过期时间1分钟", DateTime.Now.AddMinutes(1));
  51.             while (true)
  52.             {
  53.                 if (MClient.KeyExists("Key1002"))
  54.                 {
  55.                     Console.WriteLine("key:Key1002 Value:{0},当前时间:{1}", MClient.Get("Key1002"), DateTime.Now);
  56.                     Thread.Sleep(20000);
  57.                 }
  58.                 else
  59.                 {
  60.                     Console.WriteLine("key:Key1002 我已过期,当前时间:{0}", DateTime.Now);
  61.                     break;
  62.                 }
  63.             }
  64.         }
  65.     }
  66. }
复制代码



demo下载:

游客,如果您要查看本帖隐藏内容请回复





上一篇:Memcached.ClientLibrary.SockIOPool设置报错
下一篇:[CSS]去掉网页的横向滚动条,竖向滚动条
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2026-7-18 14:01

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表