历史数据保存(缓存加速)

 调用示例:

Bitmap pic = PicHistory.GetHistory(this);   
if (pic == null)
{
    pic = Sci.WebPageTool.ToBitmap(page, delay, width, height, scroll);  // 抓取网页为图像
    if(pic != null) PicHistory.SaveHistory(this, pic);
}

 

1、PicHistory.cs

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;

namespace Sci
{
    public class PicHistory
    {
        #region 历史数据处理逻辑

        /// <summary>
        /// 获取history目录
        /// </summary>
        public static string historyDir()
        {
            string dirPath = AppDomain.CurrentDomain.BaseDirectory + "history\\pic";

            //string dir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            //string dirPath = Path.Combine(dir, "history");

            if (!Directory.Exists(dirPath)) Directory.CreateDirectory(dirPath);
            return dirPath;
        }

        /// <summary>
        /// 根据id保存历史数据
        /// </summary>
        /// <param name="id"></param>
        /// <param name="data"></param>
        public static void SaveHistory(string id, Bitmap pic)
        {
            string historyPath = Path.Combine(historyDir(), id);
            pic.Save(historyPath, ImageFormat.Jpeg);
        }

        /// <summary>
        /// 获取id对应的历史数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Bitmap GetHistory(string id)
        {
            string historyPath = Path.Combine(historyDir(), id);
            if (File.Exists(historyPath))
            {
                DateTime now = DateTime.Now;
                long second = now.AddSeconds(1).Ticks - now.Ticks;

                long nowSecond = DateTime.Now.Ticks / 10000000;
                long preSecond = new FileInfo(historyPath).LastWriteTime.Ticks / 10000000;

                if (nowSecond - preSecond > 60 * 60) return null;                 // 若当前时间距离上次修改超出1个小时,则不使用历史数据
                else return Bitmap.FromFile(historyPath) as Bitmap;    // 读取历史数据内容
            }
            return null;
        }

        /// <summary>
        /// 将data保存至_page对应的历史数据中
        /// </summary>
        /// <param name="_page"></param>
        /// <param name="data"></param>
        public static void SaveHistory(Page _page, Bitmap data)
        {
            string url = _page.Request.Url.ToString();
            string id = "p" + url.GetHashCode() + ".jpg";            // 每个页面url对应一个唯一的id

            SaveHistory(id, data);
        }

        /// <summary>
        /// 获取_page对应的历史数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Bitmap GetHistory(Page _page)
        {
            string url = _page.Request.Url.ToString();
            string id = "p" + url.GetHashCode() + ".jpg";              // 每个页面url对应一个唯一的id

            return GetHistory(id);
        }

        #endregion

    }
}

 2、PageHistory.cs

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

namespace Sci
{
    public class PageHistory
    {
        #region 历史数据处理逻辑

        /// <summary>
        /// 获取history目录
        /// </summary>
        public static string historyDir()
        {
            string dirPath = AppDomain.CurrentDomain.BaseDirectory + "history";

            //string dir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            //string dirPath = Path.Combine(dir, "history");

            if (!Directory.Exists(dirPath)) Directory.CreateDirectory(dirPath);
            return dirPath;
        }

        /// <summary>
        /// 根据id保存历史数据
        /// </summary>
        /// <param name="id"></param>
        /// <param name="data"></param>
        public static void SaveHistory(string id, string data)
        {
            string historyPath = Path.Combine(historyDir(), id);
            File.WriteAllText(historyPath, data, Encoding.Default);
        }

        /// <summary>
        /// 获取id对应的历史数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static string GetHistory(string id)
        {
            string historyPath = Path.Combine(historyDir(), id);
            if (File.Exists(historyPath))
            {
                DateTime now = DateTime.Now;
                long second = now.AddSeconds(1).Ticks - now.Ticks;

                long nowSecond = DateTime.Now.Ticks / 10000000;
                long preSecond = new FileInfo(historyPath).LastWriteTime.Ticks / 10000000;

                if (nowSecond - preSecond > 60 * 60 * 24 * 7) return "";        // 若当前时间距离上次修改超出7天,则不使用历史数据
                else return File.ReadAllText(historyPath, Encoding.Default);    // 读取历史数据内容
            }
            return "";
        }

        /// <summary>
        /// 将data保存至_page对应的历史数据中
        /// </summary>
        /// <param name="_page"></param>
        /// <param name="data"></param>
        public static void SaveHistory(Page _page, string data)
        {
            string url = _page.Request.Url.ToString();
            string id = MD5.Encrypt(url);               // 每个页面url对应一个唯一的id

            SaveHistory(id, data);
        }

        /// <summary>
        /// 获取_page对应的历史数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static string GetHistory(Page _page)
        {
            string url = _page.Request.Url.ToString();
            string id = MD5.Encrypt(url);               // 每个页面url对应一个唯一的id

            return GetHistory(id);
        }

        #endregion

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值