当前位置:网站首页>C# 对象存储
C# 对象存储
2022-07-05 12:55:00 【猿长大人】
C# 对象存储
文章目录
前言
开发时经常会遇到需要保存配置的情况,最常见的实现方式是将对象序列化成Json,再写入文件并保存到本地磁盘。
本文将使用开源库ApeFree.DataStore来替换原有的对象存储过程,实现一个可以随意切换存储方式的对象存储方法。
关于DataStore
ApeFree.DataStore是一款可配置的对象存储库,支持在不同平台/介质中对内存中的对象进行存储与还原(如本地存储、注册表存储)。支持配置序列化格式(如Json、Xml),支持配置压缩算法(如GZip、Defalte),支持配置加密算法(如AES、RSA)。
开源地址:https://github.com/landriesnidis/ApeFree.DataStore
示例代码
实体类
创建一个用于测试的实体类型,预设了初始值;
/// <summary>
/// 学生(测试实体类)
/// </summary>
public class Student
{
public long Id {
get; set; } = 2022030511;
public string Name {
get; set; } = "张三";
public DateTime DateOfBirth {
get; set; } = new DateTime(2013, 6, 1);
public string ClassName {
get; set; } = "A班";
public string Description {
get; set; } = "平平无奇的学生";
public bool IsYoungPioneer {
get; set; } = true;
public string Address {
get; set; } = "二仙桥成华大道8号";
}
创建对象存储器
示例一、Json格式的本地存储器
// 本地存储配置(默认使用Json格式)
var settings = new LoaclStoreAccessSettings("./config/student.conf");
// 本地存储器
IStore<Student> store = StoreFactory.Factory.CreateLoaclStore<Student>(settings);
示例二、Xml格式的本地存储器
// 本地存储配置
var settings = new LoaclStoreAccessSettings("./config/student.conf") {
SerializationAdapter = new XmlSerializationAdapter()
};
// 本地存储器
IStore<Student> store = StoreFactory.Factory.CreateLoaclStore<Student>(settings);
示例三、Xml格式(采用GZip压缩算法)的本地存储器
// 本地存储配置
var settings = new LoaclStoreAccessSettings("./config/student.conf") {
SerializationAdapter = new XmlSerializationAdapter(),
CompressionAdapter = new GZipCompressionAdapter(),
};
// 本地存储器
IStore<Student> store = StoreFactory.Factory.CreateLoaclStore<Student>(settings);
示例四、Xml格式(采用AES加密)的本地存储器
ASE密钥:12345678901234567890123456789012
AES向量:0123456789abcdef
// 本地存储配置
var settings = new LoaclStoreAccessSettings("./config/student.conf") {
SerializationAdapter = new XmlSerializationAdapter(),
EncryptionAdapter = new AesEncryptionAdapter("12345678901234567890123456789012".GetBytes(), "0123456789abcdef".GetBytes()),
};
// 本地存储器
IStore<Student> store = StoreFactory.Factory.CreateLoaclStore<Student>(settings);
示例五、Xml格式(Deflate+AES)的注册表存储器
注意Deflate+AES纯粹是为了演示配置的用法:
// 注册表存储配置
var settings = new RegistryStoreAccessSettings(RegistryHive.CurrentUser, @"ApeFree\DataStore\Demo","student") {
SerializationAdapter = new XmlSerializationAdapter(),
CompressionAdapter = new DeflateCompressionAdapter(),
EncryptionAdapter = new AesEncryptionAdapter("12345678901234567890123456789012".GetBytes(), "0123456789abcdef".GetBytes()),
};
// 注册表存储器
IStore<Student> store = StoreFactory.Factory.CreateRegistryStore<Student>(settings);
测试窗体

public partial class EditForm : Form
{
private IStore<Student> store;
public EditForm(IStore<Student> store) : this()
{
this.store = store;
tsmiLoad.PerformClick();
}
private EditForm()
{
InitializeComponent();
}
private void tsmiLoad_Click(object sender, EventArgs e)
{
store.Load();
propertyGrid.SelectedObject = store.Value;
}
private void tsmiSave_Click(object sender, EventArgs e)
{
store.Save();
Close();
}
private void tsmiTestIO_Click(object sender, EventArgs e)
{
int times = 1000;
Stopwatch watch = new Stopwatch();
watch.Restart();
for (int i = 0; i < times; i++)
{
store.Load();
store.Save();
}
watch.Stop();
// 计算耗时(毫秒)
var elapsedTime = watch.ElapsedTicks * 1000.0 / Stopwatch.Frequency;
MessageBox.Show($"存取{
times}次测试完毕。\r\n" +
$"总耗时:{
elapsedTime}毫秒。\r\n" +
$"平均单次读取+保存耗时:{
elapsedTime / times}毫秒");
}
}
边栏推荐
- Solve Unicode decodeerror: 'GBK' codec can't decode byte 0xa2 in position 107
- I'm doing open source in Didi
- ASEMI整流桥HD06参数,HD06图片,HD06应用
- 初次使用腾讯云,解决只能使用webshell连接,不能使用ssh连接。
- 155. Minimum stack
- 解决 UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xa2 in position 107
- RHCSA2
- Discussion on error messages and API versions of SAP ui5 getsaplogonlanguage is not a function
- APICloud Studio3 API管理与调试使用教程
- MySQL splits strings for conditional queries
猜你喜欢

将函数放在模块中

RHCAS6

Introduction to the principle of DNS

CF:A. The Third Three Number Problem【关于我是位运算垃圾这个事情】

国内市场上的BI软件,到底有啥区别

Research: data security tools cannot resist blackmail software in 60% of cases

It's too convenient. You can complete the code release and approval by nailing it!

I'm doing open source in Didi

精彩速递|腾讯云数据库6月刊

蜀天梦图×微言科技丨达梦图数据库朋友圈+1
随机推荐
Notion 类笔记软件如何选择?Notion 、FlowUs 、Wolai 对比评测
Concurrent performance test of SAP Spartacus with JMeter
Research: data security tools cannot resist blackmail software in 60% of cases
UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xe6 in position 76131: invalid continuation byt
HiEngine:可媲美本地的云原生内存数据库引擎
uni-app开发语音识别app,讲究的就是简单快速。
石臻臻的2021总结和2022展望 | 文末彩蛋
MSTP and eth trunk
MySQL splits strings for conditional queries
Principle and performance analysis of lepton lossless compression
从外卖点单浅谈伪需求
Taobao product details API | get baby SKU, main map, evaluation and other API interfaces
数据湖(七):Iceberg概念及回顾什么是数据湖
Sorry, we can't open xxxxx Docx, because there is a problem with the content (repackaging problem)
I'm doing open source in Didi
Cf:a. the third three number problem
#yyds干货盘点# 解决名企真题:搬圆桌
RHCSA10
RHCSA2
mysql拆分字符串做条件查询