缓存技术,封装好的缓存类

88 篇文章 0 订阅
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Admin.Helper
{
    /// <summary>
    /// .net 本身自带的cache相关方法
    /// </summary>
    public static class CacheHelper
    {
        /// <summary>
        /// fileds
        /// </summary>
        private static System.Web.Caching.Cache ObjCache = System.Web.HttpRuntime.Cache;

        #region Exist方法
        /// <summary>
        /// 判断指定Key的缓存是否存在
        /// </summary>
        /// <param name="Key"></param>
        /// <returns></returns>
        public static bool Exist(string Key)
        {
            if (ObjCache[Key] == null)
            {
                return false;
            }
            return true;
        }
        #endregion

        #region  Get方法
        /// <summary>
        /// 获得指定Key的缓存对象
        /// </summary>
        /// <param name="Key"></param>
        /// <returns></returns>
        public static Object Get(string Key)
        {
            object objkey = null;
            if (ObjCache[Key] != null)
            {
                objkey = ObjCache.Get(Key);
            }
            return objkey;
        }
        #endregion

        #region Set方法

        /// <summary>
        /// 设置缓存
        /// </summary>
        /// <param name="Key">Cache Key</param>
        /// <param name="expiry">缓存时间</param>
        /// <param name="obj">缓存对象</param>
        public static void Set(string Key, DateTime expiry, object obj)
        {
            if (ObjCache[Key] != null)
            {
                ObjCache.Remove(Key);
            }

            ObjCache.Insert(Key, obj, null, expiry, TimeSpan.Zero);
        }

        /// <summary>
        /// 设置缓存
        /// </summary>
        /// <param name="Key">Cache Key</param>
        /// <param name="min">缓存时间【分钟】</param>
        /// <param name="obj">缓存对象</param>
        public static void Set(string Key, int min, object obj)
        {
            double douNum = double.Parse(min.ToString());
            Set(Key, DateTime.Now.AddMinutes(douNum), obj);
        }
        #endregion

        #region Del方法

        /// <summary>
        /// 删除指定Key的缓存
        /// </summary>
        /// <param name="Key"></param>
        public static void Del(string Key)
        {
            if (ObjCache[Key] != null)
            {
                ObjCache.Remove(Key);
            }
        }
        #endregion

        #region 其他

        /// <summary>
        /// 获取缓存中的项数
        /// </summary>
        public static int Count
        {
            get
            {
                return ObjCache.Count;
            }
        }

        /// <summary>
        /// 获取可用于缓存的千字节数
        /// </summary>
        public static long PrivateBytes
        {
            get
            {
                return ObjCache.EffectivePrivateBytesLimit;
            }
        }
        #endregion
    }
}
<pre code_snippet_id="155522" snippet_file_name="blog_20140114_2_3856905" name="code" class="csharp">  使用方法如下:

/// <summary>
        /// 获取产品详情
        /// </summary>
        /// <param name="proId"></param>
        /// <returns></returns>
        public static ProductDto GetProduct(string proId)
        {
            var key = "_ProductId" + proId;
            //通过key找到缓存的对象
            var cache = Helper.CacheHelper.Get(key);
            if (cache != null)
            {
                return (ProductDto)cache;
            }
            var pro = ServiceLocator.Create<IProductService>().Get(proId);
            if (pro == null) return new ProductDto();
            Helper.CacheHelper.Set(key, DateTime.Now.AddHours(8), pro);
            return pro;
        }

 
 

                
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
#import <Foundation/Foundation.h> typedef void(^SuccessBlock)(NSDictionary *returnDic, NSString *msg); typedef void(^FailureBlock)(NSString *errorInfo); typedef void(^LoadProgress)(float progress); @interface CBCacheHttpRequet : NSObject /** * Get请求 不对数据进行缓存 * * @param urlStr url * @param success 成功的回调 * @param failure 失败的回调 */ +(void)getRequestUrlStr:(NSString *)urlStr success:(SuccessBlock)success failure:(FailureBlock)failure; /** * Get请求 对数据进行缓存 * * @param urlStr url * @param success 成功的回调 * @param failure 失败的回调 */ +(void)getRequestCacheUrlStr:(NSString *)urlStr success:(SuccessBlock)success failure:(FailureBlock)failure ; /** * Post请求 不对数据进行缓存 * * @param urlStr url * @param parameters post参数 * @param success 成功的回调 * @param failure 失败的回调 */ +(void)postRequestUrlStr:(NSString *)urlStr withDic:(NSDictionary *)parameters success:(SuccessBlock )success failure:(FailureBlock)failure; /** * Post请求 对数据进行缓存 * * @param urlStr url * @param parameters post参数 * @param success 成功的回调 * @param failure 失败的回调 */ +(void)postRequestCacheUrlStr:(NSString *)urlStr withDic:(NSDictionary *)parameters success:(SuccessBlock )success failure:(FailureBlock)failure; /** * 上传单个文件 * * @param urlStr 服务器地址 * @param parameters 参数 * @param imageStr 上传的key * @param imageData 上传的问件 * @param loadProgress 上传的进度 * @param success 成功的回调 * @param failure 失败的回调 */ +(void)upLoadDataWithUrlStr:(NSString *)urlStr withDic:(NSDictionary *)parameters imageKey:(NSString *)imageStr withImageData:(NSData *)imageData upLoadProgress:(LoadProgress)loadProgress success:(SuccessBlock)success failure:(FailureBlock)failure; //清除接口数据缓存 +(void)deleteCache; @end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bingo_BIG

你的鼓励是我最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值