C#操作Memcached缓存

Memcached缓存 的安装和简单操作(add / get / remove)可以先看看这个网站

https://www.runoob.com/memcached/window-install-memcached.html

本人菜鸟,举一个简单的C#代码例子:

比如说,收到一个验证码, 在1分钟内有效,我们可以用memcache缓存来记录信息,过期时间为1分钟, 当用户在1分钟内使用验证码,提示验证通过, 超时之后提示close 。

首先建立一个普通的控制台C#程序:

 然后用nuget 工具下载  EnyimMemcached 和  Newtonsoft.Json 两个工具 . 

然后开始编辑   App.config ,将memcache的配置文件写好:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    
	<configSections>
		<sectionGroup name="enyim.com">
			<section name="memcached" type="Enyim.Caching.Configuration.MemcachedClientSection, Enyim.Caching" />
		</sectionGroup>
	</configSections>
	
	<enyim.com>
		<memcached protocol="Text">
			<servers>
				<add address="127.0.0.1" port="11211" /> 
			</servers>
			<socketPool minPoolSize="10" maxPoolSize="20" />
		</memcached>
	</enyim.com>

	<startup>
		<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
	</startup>

</configuration>

然后编辑memcache的代码文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Enyim.Caching;



namespace MemcacheTest
{
    public class MemcacheHelp
    {
        private static readonly MemcachedClient uniqueClient = new MemcachedClient();

        public static bool Set(string key, string value, DateTime expire)
        {
            var data = uniqueClient.Get(key);

            if (data == null )
            {
                return uniqueClient.Store(Enyim.Caching.Memcached.StoreMode.Add, key, value, expire);
            }
            else
            {
                return uniqueClient.Store(Enyim.Caching.Memcached.StoreMode.Replace, key, value, expire);
            }
        }

        public static object Get(string key)
        {
            return uniqueClient.Get(key);
        }

        public static bool Remove(string key)
        {
            return uniqueClient.Remove(key);
        }

    }




}

再写一个验证码的class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; 

namespace MemcacheTest
{


    /// <summary>
    /// 可用验证码 
    /// </summary>
    public class AvailableVerificationCode
    { 

        public string VerificationCode { get; set; }
        public DateTime CreateTime { get; set; }
        public DateTime EndTime { get; set; }

        public AvailableVerificationCode(string code, DateTime begin, DateTime end)
        {
            this.VerificationCode = code;
            this.CreateTime = begin;
            this.EndTime = end;
        }
         
    }



}

最后就可以编辑主程序了:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace MemcacheTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Available Verification Code");
            //测试例子:比如说一个验证码在1分钟内有效使用
            AvailableVerificationCode c1 = new AvailableVerificationCode("10068", DateTime.Now, DateTime.Now.AddMinutes(1));

            string k = string.Format("Verif_Code_{0}", c1.VerificationCode);
            string v = JsonConvert.SerializeObject(c1);

            //存放到缓存
            MemcacheHelp.Set(k, v, c1.EndTime);

            for (int i = 0; i < 15; i++)
            {
                System.Threading.Thread.Sleep(10000);
                //每隔10秒查看一次验证码
                var info = MemcacheHelp.Get(k);
                if (info != null)
                {
                    Console.WriteLine(info.ToString());
                }
                else
                {
                    Console.WriteLine("key:" + k + " is close !");
                }
            }

            //结束程序
            Console.ReadLine();
        }
    }


}

运行程序的过程中也可以用cmd黑框框来访问

telnet 127.0.0.1 11211

stats 

 通过stats里面的cur_item 就知道有多少在使用了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值