• 创建本地缓存


    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[]>>();
            }
        }
    }

  • 相关阅读:
    CentOS7搭建elasticsearch集群
    Centos7搭建redis集群及安装sentinel
    编译安装haproxy开启支持SSL
    CentOS7单节点部署redis主从复制和sentinel
    CentOS7单节点部署redis-cluster
    搭建hadoop集群
    配置nginx为FastDFS的storage server提供http访问接口
    FastDFS分布式存储
    一键部署Kubernetes高可用集群
    一键部署ETCD集群脚本
  • 原文地址:https://www.cnblogs.com/zwyAndDong/p/7371626.html
Copyright © 2020-2023  润新知