Redis缓存、MemCached和.Net内部缓存的切换使用

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介: 原文:Redis缓存、MemCached和.Net内部缓存的切换使用 接口文件: //IDataCache.
原文: Redis缓存、MemCached和.Net内部缓存的切换使用

接口文件:

//IDataCache.cs

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

namespace ypsuit.common
{
    public interface IDataCache
    {
        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="key">缓存key</param>
        /// <returns></returns>
        T Get<T>(string key);
        T Get<T>(string key,string depFile);

        /// <summary>
        /// 写入缓存
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="key">缓存key</param>
        /// <param name="value">缓存值</param>
        /// <returns>返回值,表示:是否写入成功</returns>
        bool Set<T>(string key, T value);

        /// <summary>
        /// 写入缓存,设置过期时间点
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="key">缓存key</param>
        /// <param name="value">缓存值</param>
        /// <param name="expiresAt">过期时间点</param>
        /// <returns>返回值,表示:是否写入成功</returns>
        bool Set<T>(string key, T value, DateTime expiresAt);

        /// <summary>
        /// 写入缓存,设置过期秒数
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="key">缓存key</param>
        /// <param name="value">缓存值</param>
        /// <param name="expiresSecond">过期秒数</param>
        /// <returns>返回值,表示:是否写入成功</returns>
        bool Set<T>(string key, T value, int expiresSecond);

        bool Set<T>(string key, T value, string depFile);

        /// <summary>
        /// 删除缓存
        /// </summary>
        /// <param name="key">缓存key</param>
        /// <returns></returns>
        int Delete(string key);

        /// <summary>
        /// 删除多个缓存
        /// </summary>
        /// <param name="keys">缓存key数组</param>
        /// <returns></returns>
        int Delete(string[] keys);
    }
}


//实现文件
//RedisCache.cs

using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace ypsuit.common
{
    /// <summary>
    /// Redis缓存服务器
    /// 服务器和客户端下载:
    ///  https://github.com/MSOpenTech/redis/releases
    ///  https://github.com/ServiceStack/ServiceStack.Redis
    /// </summary>
    public class RedisCache : IDataCache
    {
        private static RedisClient _redis = null;
        public static RedisClient redis
        {
            get
            {
                if (_redis == null) _redis = new RedisClient("192.168.9.128", 6379);//要开启服务器才能连接
                return _redis;
            }
        }

        ~RedisCache()
        {
            //if (_redis != null) _redis.Shutdown();
        }
        
        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <typeparam name="T">类型(对象必须可序列化,否则可以作为object类型取出再类型转换,不然会报错)</typeparam>
        /// <param name="key">缓存key</param>
        /// <returns></returns>
        public T Get<T>(string key)
        {
            return redis.Get<T>(key);
        }
        public T Get<T>(string key, string depFile)
        {
            string timeKey = key + "_time";
            if (redis.Exists(timeKey) > 0 && redis.Exists(key) > 0)
            {
                DateTime obj_time = Get<DateTime>(timeKey);
                T obj_cache = Get<T>(key);
                if (File.Exists(depFile))
                {
                    FileInfo fi = new FileInfo(depFile);
                    if (obj_time != fi.LastWriteTime)
                    {
                        Delete(key);
                        Delete(timeKey);
                        return default(T);
                    }
                    else return obj_cache;
                }
                else
                {
                    throw new Exception("文件(" + depFile + ")不存在!");
                }
            }
            else return default(T);

        }

        public bool Set<T>(string key, T value)
        {
            return redis.Set<T>(key, value);
        }
        public bool Set<T>(string key, T value, DateTime expiresAt)
        {
            return redis.Set<T>(key, value, expiresAt);
        }

        public bool Set<T>(string key, T value, int expiresSecond)
        {
            return redis.Set<T>(key, value, DateTime.Now.AddSeconds(expiresSecond));
        }

        public bool Set<T>(string key, T value, string depFile)
        {
            bool ret1 = redis.Set<T>(key, value);
            if (ret1 && File.Exists(depFile))
            {
                FileInfo fi = new FileInfo(depFile);
                DateTime lastWriteTime = fi.LastWriteTime;
                return redis.Set<DateTime>(key + "_time", lastWriteTime);
            }
            return false;
        }

        public int Delete(string key)
        {
            return redis.Del(key);
        }
        public int Delete(string[] keys)
        {
            return redis.Del(keys);
        }
        public void Dispose()
        {
            if (_redis != null) _redis.Shutdown();//调用Dispose释放memcached客户端连接
        }

    }
}

//实现文件

//MemcachedCache.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Memcached.ClientLibrary;
using System.IO;

namespace ypsuit.common
{
    public class MemcachedCache:IDataCache
    {
        private MemcachedClient _mc =null; 
        protected MemcachedClient mc
        {
            get
            {
                if(_mc==null) _mc=new MemcachedClient();//初始化一个客户端 
                return _mc;
            }
        }
        /// <summary>
        /// 如果默认不是本地服务,可以额外指定memcached服务器地址
        /// </summary>
        public static string[] serverList
        {
            get;
            set;
        }
        private static MemcachedCache _instance = null;
        /// <summary>
        /// 单例实例对象,外部只能通过MemcachedHelper.instance使用memcached
        /// </summary>
        public static MemcachedCache instance
        {
            get
            {
                if (_instance == null)
                {
                    if (serverList != null && serverList.Length > 0)
                        _instance = new MemcachedCache(serverList);
                    else _instance = new MemcachedCache();
                }
                
                return _instance;
            }
        }
        SockIOPool pool;
        private void start(params string[]servers)
        {
            string[] serverlist;
            if (servers == null || servers.Length < 1)
            {
                serverlist = new string[] { "127.0.0.1:11011" }; //服务器列表,可多个
            }
            else
            {
                serverlist=servers;
            }
            pool = SockIOPool.GetInstance();

            //根据实际情况修改下面参数
            pool.SetServers(serverlist);
            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(); // initialize the pool for memcache servers      
        }
        public MemcachedCache(string[] servers)
        {
            start(servers);
        }
        public MemcachedCache()
        {
            start();
        }
        ~MemcachedCache()
        {
            //if (pool != null) pool.Shutdown();
        }

        public T Get<T>(string key)
        {
            object data = mc.Get(key);
            if (data is T) return (T)data;
            else return default(T);
        }
        public T Get<T>(string key, string depFile)
        {
            string timeKey = key + "_time";
            if (mc.KeyExists(timeKey) && mc.KeyExists(key))
            {
                DateTime obj_time = Get<DateTime>(timeKey);
                T obj_cache = Get<T>(key);
                if (File.Exists(depFile))
                {
                    FileInfo fi = new FileInfo(depFile);
                    if (obj_time != fi.LastWriteTime)
                    {
                        Delete(key);
                        Delete(timeKey);
                        return default(T);
                    }
                    else return obj_cache;
                }
                else
                {
                    throw new Exception("文件(" + depFile + ")不存在!");
                }
            }
            else return default(T);

        }
        public bool Set<T>(string key, T value)
        {
            return mc.Set(key, value);
        }
        public bool Set<T>(string key, T value, DateTime expiresAt)
        {
            return mc.Set(key, value, expiresAt);
        }

        public bool Set<T>(string key, T value, int expiresSecond)
        {
            return mc.Set(key, value, DateTime.Now.AddSeconds(expiresSecond));
        }

        public bool Set<T>(string key, T value, string depFile)
        {
            bool ret1 = mc.Set(key, value);
            if (ret1 && File.Exists(depFile))
            {
                FileInfo fi = new FileInfo(depFile);
                DateTime lastWriteTime = fi.LastWriteTime;
                return mc.Set(key + "_time", lastWriteTime);
            }
            return false;
        }


        public int Delete(string key)
        {
            return mc.Delete(key)?1:0;
        }
        public int Delete(string[] keys)
        {
            int i = 0;
            foreach (var key in keys)
            {
                mc.Delete(key);
                i++;
            }
            return i;
        }
        //在应用程序退出之前,调用Dispose释放memcached客户端连接
        public void Dispose()
        {
            if (pool != null) pool.Shutdown();
        }
    }
}

//实现文件

//RuntimeCache.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Caching;

namespace ypsuit.common
{
    public class RuntimeCache : IDataCache
    {

        #region 删除缓存
        /// <summary>
        /// 删除缓存
        /// </summary>
        /// <param name="CacheKey">键</param>
        public int Delete(string CacheKey)
        {
            HttpRuntime.Cache.Remove(CacheKey);
            return 1;
        }
        public int Delete(string[] CacheKeys)
        {
            int i = 0;
            foreach (var key in CacheKeys)
            {
                HttpRuntime.Cache.Remove(key);
                i++;
            }
            return i;
        }
        #endregion

        #region 获取缓存,依赖时间
        /// <summary>
        /// 获取缓存,依赖时间
        /// </summary>
        /// <param name="CacheKey">键</param>
        /// <returns></returns>
        public T Get<T>(string CacheKey)
        {
            object obj_time = HttpRuntime.Cache[CacheKey + "_time"];
            object obj_cache = HttpRuntime.Cache[CacheKey];
            if (obj_time != null && obj_cache != null)
            {
                if (Convert.ToDateTime(obj_time) < DateTime.Now)
                {
                    Delete(CacheKey);
                    Delete(CacheKey + "_time");
                    return default(T);
                }
                else return (T)obj_cache;
            }
            else
            {
                Delete(CacheKey);
                Delete(CacheKey + "_time");
                return default(T);
            }
        }
        #endregion

        #region 获取缓存,依赖文件
        /// <summary>
        /// 获取缓存,依赖文件
        /// </summary>
        /// <param name="CacheKey">键</param>
        /// <param name="depFile">依赖的文件</param>
        /// <returns></returns>
        public T Get<T>(string CacheKey, string depFile)
        {
            object obj_time = HttpRuntime.Cache[CacheKey + "_time"];
            object obj_cache = HttpRuntime.Cache[CacheKey];
            if (File.Exists(depFile))
            {
                FileInfo fi = new FileInfo(depFile);

                if (obj_time != null && obj_cache != null)
                {
                    if (Convert.ToDateTime(obj_time) != fi.LastWriteTime)
                    {
                        Delete(CacheKey);
                        Delete(CacheKey + "_time");
                        return default(T);
                    }
                    else return (T)obj_cache;
                }
                else
                {
                    Delete(CacheKey);
                    Delete(CacheKey + "_time");
                    return default(T);
                }
            }
            else
            {
                throw new Exception("文件(" + depFile + ")不存在!");
            }
        }
        #endregion

        #region 简单的插入缓存
        /// <summary>
        /// 简单的插入缓存
        /// </summary>
        /// <param name="CacheKey">键</param>
        /// <param name="objObject">数据</param>
        public bool Set<T>(string CacheKey, T objObject)
        {
            HttpRuntime.Cache.Insert(CacheKey, objObject);
            return true;
        }
        #endregion

        #region 有过期时间的插入缓存数据
        public bool Set<T>(string CacheKey, T objObject, DateTime expiresAt)
        {
            HttpRuntime.Cache.Insert(CacheKey, objObject, null, expiresAt, Cache.NoSlidingExpiration);
            HttpRuntime.Cache.Insert(CacheKey + "_time", expiresAt, null, expiresAt, Cache.NoSlidingExpiration);//存储过期时间
            return true;
        }
        #endregion

        #region 插入缓存数据,指定缓存多少秒
        public bool Set<T>(string CacheKey, T objObject, int seconds)
        {
            DateTime expiresAt = DateTime.Now.AddSeconds(seconds);
            HttpRuntime.Cache.Insert(CacheKey, objObject, null, expiresAt, Cache.NoSlidingExpiration);
            HttpRuntime.Cache.Insert(CacheKey + "_time", expiresAt, null, expiresAt, Cache.NoSlidingExpiration);//存储过期时间
            return true;
        }
        #endregion

        #region 依赖文件的缓存,文件没改不会过期
        /// <summary>
        /// 依赖文件的缓存,文件没改不会过期
        /// </summary>
        /// <param name="CacheKey">键</param>
        /// <param name="objObject">数据</param>
        /// <param name="depfilename">依赖文件,可调用 DataCache 里的变量</param>
        public bool Set<T>(string CacheKey, T objObject, string depfilename)
        {
            //缓存依赖对象
            System.Web.Caching.CacheDependency dep = new System.Web.Caching.CacheDependency(depfilename);
            DateTime absoluteExpiration = System.Web.Caching.Cache.NoAbsoluteExpiration;
            TimeSpan slidingExpiration = System.Web.Caching.Cache.NoSlidingExpiration;
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            objCache.Insert(
                CacheKey,
                objObject,
                dep,
                absoluteExpiration, //从不过期
                slidingExpiration, //禁用可调过期
                System.Web.Caching.CacheItemPriority.Default,
                null);
            if (File.Exists(depfilename))
            {
                FileInfo fi = new FileInfo(depfilename);
                DateTime lastWriteTime = fi.LastWriteTime;
                HttpRuntime.Cache.Insert(CacheKey + "_time", lastWriteTime, null, absoluteExpiration, slidingExpiration);//存储文件最后修改时间
            }
            return true;
        }
        #endregion
    }
}

//缓存统一入口

    public partial class DataCache
    {
        private static IDataCache _instance = null;
        /// <summary>
        /// 静态实例,外部可直接调用
        /// </summary>
        public static IDataCache Instance
        {
            get
            {
                if (_instance == null) _instance = new RuntimeCache();//这里可以改变缓存类型
                return _instance;
            }
        }
    }








相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
目录
相关文章
|
1天前
|
存储 缓存 NoSQL
Redis入门到通关之Redis缓存数据实战
Redis入门到通关之Redis缓存数据实战
|
3天前
|
存储 缓存 运维
软件体系结构 - 缓存技术(5)Redis Cluster
【4月更文挑战第20天】软件体系结构 - 缓存技术(5)Redis Cluster
136 10
|
10天前
|
缓存 NoSQL Java
使用Redis进行Java缓存策略设计
【4月更文挑战第16天】在高并发Java应用中,Redis作为缓存中间件提升性能。本文探讨如何使用Redis设计缓存策略。Redis是开源内存数据结构存储系统,支持多种数据结构。Java中常用Redis客户端有Jedis和Lettuce。缓存设计遵循一致性、失效、雪崩、穿透和预热原则。常见缓存模式包括Cache-Aside、Read-Through、Write-Through和Write-Behind。示例展示了使用Jedis实现Cache-Aside模式。优化策略包括分布式锁、缓存预热、随机过期时间、限流和降级,以应对缓存挑战。
|
18天前
|
存储 缓存 NoSQL
使用redis进行缓存加速
使用redis进行缓存加速
27 0
|
19天前
|
存储 缓存 NoSQL
Java手撸一个缓存类似Redis
`LocalExpiringCache`是Java实现的一个本地缓存类,使用ConcurrentHashMap存储键值对,并通过ScheduledExecutorService定时清理过期的缓存项。类中包含`put`、`get`、`remove`等方法操作缓存,并有`clearCache`方法来清除过期的缓存条目。初始化时,会注册一个定时任务,每500毫秒检查并清理一次过期缓存。单例模式确保了类的唯一实例。
16 0
|
1月前
|
缓存 NoSQL Java
spring cache整合redis实现springboot项目中的缓存功能
spring cache整合redis实现springboot项目中的缓存功能
46 1
|
1月前
|
存储 缓存 NoSQL
[Redis]——缓存击穿和缓存穿透及解决方案(图解+代码+解释)
[Redis]——缓存击穿和缓存穿透及解决方案(图解+代码+解释)
255 0
|
3月前
|
开发框架 前端开发 .NET
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
42 0
|
1月前
|
开发框架 前端开发 .NET
进入ASP .net mvc的世界
进入ASP .net mvc的世界
29 0
|
1月前
mvc.net分页查询案例——mvc-paper.css
mvc.net分页查询案例——mvc-paper.css
5 0

热门文章

最新文章