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,而且还很方便,于是就给出来,希望大家多提意见。



    邹健 2007-07-24 10:55 发表评论

    文章来源:http://www.cnblogs.com/chsword/archive/2007/07/24/829175.html
  • 相关阅读:
    ASP.NET Core Web API 帮助页
    SQL SERVER 被锁住的表,以及解锁。
    关键字查找相关存储过程,函数和视图
    MsSql 生成数据文档
    WebApiTestClient 接口测试页
    sql日期
    为什么同样的Sql语句在SqlServer RDS 查询得到的和自己本机SqlServer 查询的不一样呢?
    VS 无法在web服务器上启动调试。您没有调试web服务器进程的权限
    ROS学习之创建工作空间
    QT学习之forward declaration of 'struct Ui::xxx';invalid use of incomplete struct "Ui::Widget"
  • 原文地址:https://www.cnblogs.com/wallis0922/p/872322.html
Copyright © 2011-2022 走看看