创建本地缓存

using Microsoft.Practices.Prism.ViewModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Web.Caching;
using System.Windows;

namespace WcfClient
{
    public class CacheHelper : NotificationObject
    {
        private Dictionary<string, Dictionary<string, byte[]>> databaseResults;
        public Dictionary<string, Dictionary<string, byte[]>> DatabaseResults
         {
            get { return this.databaseResults; }
            set
            {
                if (this.databaseResults != value)
                {
                    this.databaseResults = value;
                    this.RaisePropertyChanged(() => this.DatabaseResults);
                }
            }
         }

         public CacheHelper()
         {
             if (File.Exists(System.Environment.CurrentDirectory + @"\Y_cache"))
             {
                 this.DatabaseResults = ReadCache(System.Environment.CurrentDirectory + @"\Y_cache");
             }
             else
             {
                 this.DatabaseResults = new Dictionary<string, Dictionary<string, byte[]>>();
             }
         }

        public void AddCache(string keyName, string query, byte[] bt)
        {
            if (!DatabaseResults.ContainsKey(keyName))
            {
                DatabaseResults.Add(keyName,
                                        new Dictionary<string, byte[]> { { query, bt } });
                WriteCache(System.Environment.CurrentDirectory + @"\Y_cache", DatabaseResults);
            }
            else
            {
                if (DatabaseResults[keyName].ContainsKey(query))
                {
                    if (DatabaseResults[keyName][query] != bt)
                    {
                        DatabaseResults[keyName][query] = bt;
                        WriteCache(System.Environment.CurrentDirectory + @"\Y_cache", DatabaseResults);
                    }
                }
                else
                {
                    DatabaseResults[keyName].Add(query, bt);
                    WriteCache(System.Environment.CurrentDirectory + @"\Y_cache", DatabaseResults);
                }
            }
        }

        public bool ClearCache(string keyName, string query)
        {
            return true;
        }

        public static void WriteCache(string filename, Dictionary<string, Dictionary<string, byte[]>> cache)
        {
            try
            {
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    DataContractSerializer serializer = new DataContractSerializer(typeof(Dictionary<string, Dictionary<string, byte[]>>));
                    serializer.WriteObject(memoryStream, cache);
                    memoryStream.Position = 0;

                    byte[] arr = EncryptDecrypt.Encrypt("legend", memoryStream.ToArray());
                    File.WriteAllBytes(filename, arr);
                }
            }
            catch
            {
                return;
            }
        }

        public static Dictionary<string, Dictionary<string, byte[]>> ReadCache(string filename)
        {
            byte[] receiveDate = EncryptDecrypt.Decrypt("legend", File.ReadAllBytes(filename));
            using (MemoryStream memoryStream = new MemoryStream(receiveDate))
            {
                DataContractSerializer deserializer = new DataContractSerializer(typeof(Dictionary<string, Dictionary<string, byte[]>>));
                memoryStream.Write(receiveDate, 0, receiveDate.Length);
                memoryStream.Position = 0;

                return (Dictionary<string, Dictionary<string, byte[]>>)deserializer.ReadObject(memoryStream);
            }
            //return new Dictionary<string, Dictionary<string, byte[]>>();
        }
    }
}

posted @ 2017-08-16 09:37  行走在0和1之间  阅读(241)  评论(0编辑  收藏  举报