zoukankan      html  css  js  c++  java
  • 自写保存字符串或文件为asp.net缓存的类

    using System;
    using System.Text;
    using System.Web;
    using System.IO;

    namespace Chsword {
        
    /// <summary>
        
    /// 成幻互联缓存类
        
    /// 邹健 2007.5
        
    /// </summary>
        class Cache {
            TimeSpan _TimeSpan;
            
    /// <summary>
            
    /// 构造函数。自动设置缓存为1小时20分。
            
    /// </summary>
            public Cache() {
                _TimeSpan 
    = new TimeSpan(012000);
            }
            
    /// <summary>
            
    /// 构造函数。手动设置缓存有效时间。
            
    /// </summary>
            
    /// <param name="ts">缓存有效时间</param>
            public Cache(TimeSpan ts) {
                _TimeSpan 
    = ts;
            }
            
    /// <summary>
            
    /// 检测缓存是否存在或为空。
            
    /// </summary>
            
    /// <param name="CacheName">缓存名称</param>
            
    /// <returns>缓存存在则返回True,反之为False。</returns>
            public Boolean IsNullorEmpty(String CacheName) {
                
    if (HttpContext.Current.Cache[CacheName] != null)
                    
    if (String.IsNullOrEmpty(HttpContext.Current.Cache[CacheName].ToString()))
                        
    return true;
                    
    else
                        
    return false;
                
    else
                    
    return true;

            }
            
    /// <summary>
            
    /// 设置缓存。
            
    /// </summary>
            
    /// <param name="CacheName">缓存名称</param>
            
    /// <returns>是否存储成功。</returns>
            public Boolean SetCache(String CacheName){
                
    try {
                    String fn 
    = HttpContext.Current.Request.MapPath(String.Format("~/Xml/{0}.xml", CacheName));
                    
    return SetCache(CacheName,OpenTextFile(fn));
                }
                
    catch {
                    
    return false;
                }
            }
            
    /// <summary>
            
    /// 设置缓存。
            
    /// </summary>
            
    /// <param name="CacheName">缓存名称</param>
            
    /// <param name="CacheValue">缓存的值</param>
            
    /// <returns>是否存储成功。</returns>
            public Boolean SetCache(String CacheName,String CacheValue) {
                
    try {
                    
    if (!IsNullorEmpty(CacheName))
                        
    return true;
                    
    else {//如果不存在,则重新载入缓存。
                        HttpContext.Current.Cache.Add(CacheName, CacheValue, null, DateTime.MaxValue, _TimeSpan , System.Web.Caching.CacheItemPriority.Normal, null);
                        
    return true;
                    }
                }
                
    catch {
                    
    return false;
                }
            }
            
    /// <summary>
            
    /// 打开文本文件,并返回文件内容。
            
    /// </summary>
            
    /// <param name="fn">文件路径。</param>
            
    /// <returns>返回文本文件内容。</returns>
            String OpenTextFile(String fn) {
                String text;
                
    using (StreamReader sr = new StreamReader(fn, System.Text.Encoding.UTF8)) {
                    text 
    = sr.ReadToEnd();
                }
                
    return text;
            }
        }
    }

    在使用时要先进行Cache的实例化,再进行实例化。
    Boolean SetCache(String CacheName)这个函数是为我的工程特制的,如果可以的话呢,可以对其进行重写。

    本身这段代码并没有什么技术含量,写了这么长时间,用着也没有BUG,而且还很方便,于是就给出来,希望大家多提意见。

  • 相关阅读:
    自学Aruba6.3-账号管理(web页面配置)
    自学Aruba6.2-控制器基本维护操作(web页面配置)
    自学Aruba6.1-基本网络参数配置(web页面配置)
    自学Aruba5.1.2-带宽限制
    自学Aruba5.1.1-基于时间的Role定义
    自学Linux Shell19.2-gawk程序高级特性
    自学Linux Shell19.1-gawk程序基础特性
    自学Linux Shell18.3-sed实用工具
    自学Linux Shell18.2-sed编辑器高级特性
    js 数组API之every、some用法
  • 原文地址:https://www.cnblogs.com/chsword/p/829175.html
Copyright © 2011-2022 走看看