• 183 阅读
  • 2 回复

enyim memcached

视频在线上传+队列转换FLV+水印+捉图+修复+获时+转3GP(API语言不限,开视频站必备!)

代码 复制 - 运行

  <configSections>


    <sectionGroup name="enyim.com">
      <section name="memcached" type="Enyim.Caching.Configuration.MemcachedClientSection, Enyim.Caching" />
    </sectionGroup>

  </configSections>
  <!--地址重写规则2 从"~/config/rewrite.config" 移到BIN目录,为了修改即会生效-->
  <rewriter file="~/bin/UrlRewriter.config"/>

    <!--Eniym配置开始-->
  <enyim.com>
    <memcached protocol="Binary">
      <servers>
        <!-- make sure you use the same ordering of nodes in every configuration you have -->
        <add address="127.0.0.1" port="11211" />
      </servers>
      <socketPool minPoolSize="10" maxPoolSize="20" connectionTimeout="00:00:10" deadTimeout="00:00:10" />
      <keyTransformer type="Enyim.Caching.Memcached.TigerHashKeyTransformer, Enyim.Caching" />
    </memcached>
  </enyim.com>
  <!--Eniym配置结束-->
小鱼的淘宝店铺-多多支持哇
视频在线上传+队列转换FLV+水印+捉图+修复+获时+转3GP(API语言不限,开视频站必备!)

代码 复制 - 运行

using Enyim.Caching;
using Enyim.Caching.Configuration;
using Enyim.Caching.Memcached;
using System;
using System.Net;

namespace DTcms.Web.test
{
    public partial class Memcached : System.Web.UI.Page
    {
        public class MemCachedHelper
        {
            private static MemcachedClient MemClient;
            static readonly object padlock = new object();

            //线程安全的单例模式
            public static MemcachedClient getInstance()
            {
                if (MemClient == null)
                {
                    lock (padlock)
                    {
                        if (MemClient == null)
                        {
                            MemClientInit();
                        }
                    }
                }
                return MemClient;
            }

            static void MemClientInit()
            {
                try
                {
                    MemClient = new MemcachedClient();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                ////初始化缓存
                //MemcachedClientConfiguration memConfig = new MemcachedClientConfiguration();
                //IPAddress newaddress = IPAddress.Parse(Dns.GetHostEntry("m-uf6c0904a79ac664.memcache.rds.aliyuncs.com").AddressList[0].ToString());//your_ocs_host替换为OCS内网地址
                //IPEndPoint ipEndPoint = new IPEndPoint(newaddress, 11211);
                //// 配置文件 - ip
                //memConfig.Servers.Add(ipEndPoint);
                //// 配置文件 - 协议
                //memConfig.Protocol = MemcachedProtocol.Binary;
                //// 配置文件-权限
                //memConfig.Authentication.Type = typeof(PlainTextAuthenticator);
                //memConfig.Authentication.Parameters["zone"] = "ksbbs";
                //memConfig.Authentication.Parameters["userName"] = "";
                //memConfig.Authentication.Parameters["password"] = "";
                ////下面请根据实例的最大连接数进行设置
                //memConfig.SocketPool.MinPoolSize = 5;
                //memConfig.SocketPool.MaxPoolSize = 200;
                ////MemClient = new MemcachedClient(memConfig);
                //MemClient = new MemcachedClient(memConfig);

            }

            /// <summary>
            /// 插入指定值
            /// </summary>
            /// <param name="key">缓存名称 </param>
            /// <param name="value">缓存值</param>
            /// <param name="minutes">过期时间(分钟),默认一个礼拜</param>
            /// <returns>返回是否成功</returns>
            public bool Insert(string key, string value, int minutes = 10080)
            {
                MemcachedClient mc = getInstance();
                var data = mc.Get(key);

                DateTime dateTime = DateTime.Now.AddMinutes(1);
                if (data == null)
                    return mc.Store(StoreMode.Add, key, value, dateTime);
                else
                    return mc.Store(StoreMode.Replace, key, value, dateTime);
            }
            public bool set(string key, string value, int minutes = 10080)
            {
                MemcachedClient mc = getInstance();
                var data = mc.Get(key);

                DateTime dateTime = DateTime.Now.AddMinutes(minutes);
                if (data == null)
                    return mc.Store(StoreMode.Set, key, value, DateTime.Now.AddMinutes(20));
                else
                    return mc.Store(StoreMode.Replace, key, value, DateTime.Now.AddMinutes(20));
            }
            /// <summary>
            /// 获取缓存值
            /// </summary>
            /// <param name="key"></param>
            /// <returns></returns>
            public object Get(string key)
            {
                MemcachedClient mc = getInstance();
               return mc.Get(key);
                
            }

            /// <summary>
            /// 删除指定缓存
            /// </summary>
            /// <param name="key"></param>
            /// <returns></returns>
            public bool Remove(string key)
            {
                MemcachedClient mc = getInstance();

                return mc.Remove(key);
            }

            /// <summary>
            /// 清空缓存服务器上的缓存
            /// </summary>
            public void FlushCache()
            {
                MemcachedClient mc = getInstance();

                mc.FlushAll();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //string pageCacheKey = "sbbbdn";
            //MemCachedHelper memcacheHelper = new MemCachedHelper();

            ////Int32 _VerificationCode = memcacheHelper.Get<Int32>(pageCacheKey);
            //object _VerificationCode = memcacheHelper.Get(pageCacheKey);
            //if (_VerificationCode != null)//存在
            //{
            //    Response.Write("存在:" + _VerificationCode.ToString());
            //    Response.End();
            //}
            //else
            //{
            //    if (memcacheHelper.set(pageCacheKey, "123"))
            //    {
            //        Response.Write("写入成功");
            //    }
            //    else
            //    {
            //        Response.Write("写入失败");
            //    }
            //    Response.End();
            //}

            
            //Response.End();
            // create a MemcachedClient in your application 
            // you can cache the client in a static variable or just recreate it every time
            MemcachedClient mc = new MemcachedClient();

            // store a string in the cache
            mc.Store(StoreMode.Set, "MyKey", "Hello");

            // retrieve the item from the cache
            Response.Write(mc.Get("MyKey")+"</br>");

            // store some other items
            mc.Store(StoreMode.Set, "D1", 1234L);
            mc.Store(StoreMode.Set, "D2", DateTime.Now);
            mc.Store(StoreMode.Set, "D3", true);
            mc.Store(StoreMode.Set, "D4", new Product());

            mc.Store(StoreMode.Set, "D5", new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
            Response.Write("D1: {0}"+mc.Get("D1") + "</br>");
            Response.Write("D2: {0}"+ mc.Get("D2") + "</br>");
            Response.Write("D3: {0}"+mc.Get("D3") + "</br>");
            Response.Write("D4: {0}"+mc.Get("D4") + "</br>");

            byte[] tmp = mc.Get<byte[]>("D5");

            // delete them from the cache
            mc.Remove("D1");
            mc.Remove("D2");
            mc.Remove("D3");
            mc.Remove("D4");

            // add an item which is valid for 10 mins
            mc.Store(StoreMode.Set, "D4", new Product(), new TimeSpan(0, 10, 0));
            Response.Write("D4: "+ mc.Get("D4") + "</br>");
            Response.End();
            
        }
    }

    // objects must be serializable to be able to store them in the cache
    [Serializable]
    class Product
    {
        public double Price = 1.24;
        public string Name = "Mineral Water";

        public override string ToString()
        {
            return String.Format("Product {{{0}: {1}}}", this.Name, this.Price);
        }
    }
}
小鱼的淘宝店铺-多多支持哇
视频在线上传+队列转换FLV+水印+捉图+修复+获时+转3GP(API语言不限,开视频站必备!)
Enyim.Caching.rar (您是游客您没有权限下载)



注意:在设置过期时间时是无效的,建议增加健值:
         Int32 _VerificationCode = interfaceCache.Get<Int32>(pageCacheKey);
               Int32 _VerificationCodeExpires = interfaceCache.Get<Int32>(pageCacheKey + "_Expires");





if (timeSpanSeconds(StampToDateTime(_VerificationCodeExpires.ToString())) < 1 * 60)//60秒内还未过期
               {

}
小鱼的淘宝店铺-多多支持哇