zoukankan      html  css  js  c++  java
  • 创建本地缓存

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

  • 相关阅读:
    在VS2008中使用WSE 3.0【转】
    .Net调用Java端带有WS-Security支持的Web Service各方案实战【转】
    Java与WCF交互(一):Java客户端调用WCF服务 【转】
    Java与WCF交互(二):WCF客户端调用Java web service【转】
    c#调用带有安全认证的java webservice
    利用Web Services开发分布式应用
    注册dll文件
    Oracle:"ORA-00942: 表或视图不存在"
    sql_server角色成员身份权限
    10013: 以一种访问权限不允许的方式做了一个访问套接字的尝试【WCF异常】
  • 原文地址:https://www.cnblogs.com/zwyAndDong/p/7371626.html
Copyright © 2011-2022 走看看