zoukankan      html  css  js  c++  java
  • CacheHelper

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

    namespace DC.Website.MVC5.Helpers
    {
    public class CacheHelper
    {
    /// <summary>
    /// 创建缓存项的文件依赖
    /// </summary>
    /// <param name="key">缓存Key</param>
    /// <param name="obj">object对象</param>
    /// <param name="fileName">文件绝对路径</param>
    public static void Insert(string key, object obj, string fileName)
    {
    //创建缓存依赖项
    CacheDependency dep = new CacheDependency(fileName);
    //创建缓存
    HttpContext.Current.Cache.Insert(key, obj, dep);
    }

    /// <summary>
    /// 创建缓存项过期
    /// </summary>
    /// <param name="key">缓存Key</param>
    /// <param name="obj">object对象</param>
    /// <param name="expires">过期时间(分钟)</param>
    public static void Insert(string key, object obj, int expires)
    {
    HttpContext.Current.Cache.Insert(key, obj, null, Cache.NoAbsoluteExpiration, new TimeSpan(0, expires, 0));
    }

    /// <summary>
    /// 获取缓存对象
    /// </summary>
    /// <param name="key">缓存Key</param>
    /// <returns>object对象</returns>
    public static object Get(string key)
    {
    if (string.IsNullOrEmpty(key))
    {
    return null;
    }
    return HttpContext.Current.Cache.Get(key);
    }

    /// <summary>
    /// 获取缓存对象
    /// </summary>
    /// <typeparam name="T">T对象</typeparam>
    /// <param name="key">缓存Key</param>
    /// <returns></returns>
    public static T Get<T>(string key)
    {
    object obj = Get(key);
    return obj == null ? default(T) : (T)obj;
    }

    }
    }

    萌橙 你瞅啥?
  • 相关阅读:
    Python开发【第六篇】循环语句
    Python开发【第四篇】语句与函数
    Python开发【第三篇】数据类型
    Python开发【第二篇】:初始Python
    2019-10-11入博客第一篇文章
    设计模式——命令模式
    设计模式——职责链模式
    设计模式——单例模式
    设计模式——原型模式
    设计模式——建造者模式
  • 原文地址:https://www.cnblogs.com/daimaxuejia/p/7735189.html
Copyright © 2011-2022 走看看