You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
154 lines
5.0 KiB
C#
154 lines
5.0 KiB
C#
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Runtime.Serialization.Formatters.Binary;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Estsh.Client.Common.Util
|
|
{
|
|
public class LocalCache
|
|
{
|
|
private static Dictionary<string, string> _cache = new Dictionary<string, string>();
|
|
/// <summary>
|
|
/// 本地缓存的数据
|
|
/// </summary>
|
|
public static Dictionary<string, string> localData
|
|
{
|
|
get { return _cache; }
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 读取缓存
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static void ReadCache()
|
|
{
|
|
Dictionary<String, String> cacheData = new Dictionary<String, String>();
|
|
string lineStr;
|
|
using (FileStream fs = new FileStream("local.data", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
|
|
{
|
|
using (StreamReader sr = new StreamReader(fs, Encoding.UTF8))
|
|
{
|
|
|
|
while ((lineStr = sr.ReadLine()) != null)
|
|
{
|
|
cacheData.Add(lineStr.Split("=")[0], lineStr.Split("=")[1]);
|
|
}
|
|
}
|
|
_cache = cacheData;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 将数据缓存到本地
|
|
/// </summary>
|
|
/// <param name="key"></param>
|
|
/// <param name="value"></param>
|
|
public static void AddCache(string key,string value)
|
|
{
|
|
Dictionary<String, String> cacheData = new Dictionary<String, String>();
|
|
cacheData.Add(key, value);
|
|
AddCache(cacheData);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 将数据缓存到本地文件
|
|
/// </summary>
|
|
/// <param name="data"></param>
|
|
public static void AddCache(Dictionary<string, string> data)
|
|
{
|
|
Dictionary<String, String> cacheData = new Dictionary<String, String>();
|
|
using (FileStream fs = new FileStream("local.data", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
|
|
{
|
|
string lineStr;
|
|
using (StreamReader sr = new StreamReader(fs, Encoding.UTF8))
|
|
{
|
|
|
|
while ((lineStr = sr.ReadLine()) != null)
|
|
{
|
|
cacheData.Add(lineStr.Split("=")[0], lineStr.Split("=")[1]);
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach (var item in data)
|
|
{
|
|
if (cacheData.ContainsKey(item.Key))
|
|
{
|
|
cacheData[item.Key] = data[item.Key].ToString();
|
|
}
|
|
else
|
|
{
|
|
cacheData.Add(item.Key, item.Value.ToString());
|
|
}
|
|
}
|
|
|
|
using (FileStream fs = new FileStream("local.data", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
|
|
{
|
|
fs.Seek(0, SeekOrigin.Begin);
|
|
fs.SetLength(0);
|
|
|
|
using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8))
|
|
{
|
|
foreach (var item in cacheData)
|
|
{
|
|
sw.WriteLine(item.Key + "=" + item.Value);
|
|
}
|
|
|
|
}
|
|
}
|
|
_cache = cacheData;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 将删除本地缓存的数据
|
|
/// </summary>
|
|
/// <param name="data"></param>
|
|
public static void DeleteCache(Dictionary<string, string> data)
|
|
{
|
|
Dictionary<String, String> cacheData = new Dictionary<String, String>();
|
|
using (FileStream fs = new FileStream("local.data", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
|
|
{
|
|
string lineStr;
|
|
using (StreamReader sr = new StreamReader(fs, Encoding.UTF8))
|
|
{
|
|
|
|
while ((lineStr = sr.ReadLine()) != null)
|
|
{
|
|
cacheData.Add(lineStr.Split("=")[0], lineStr.Split("=")[1]);
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach (var item in data)
|
|
{
|
|
if (cacheData.ContainsKey(item.Key))
|
|
{
|
|
cacheData.Remove(item.Key);
|
|
}
|
|
}
|
|
|
|
using (FileStream fs = new FileStream("local.data", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
|
|
{
|
|
fs.Seek(0, SeekOrigin.Begin);
|
|
fs.SetLength(0);
|
|
|
|
using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8))
|
|
{
|
|
foreach (var item in cacheData)
|
|
{
|
|
sw.WriteLine(item.Key + "=" + item.Value);
|
|
}
|
|
|
|
}
|
|
}
|
|
_cache = cacheData;
|
|
}
|
|
|
|
|
|
}
|
|
}
|