当前位置:网站首页>C object storage
C object storage
2022-07-05 13:14:00 【Ape master】
C# Object storage
List of articles
Preface
When developing, we often encounter the need to save the configuration , The most common implementation is to serialize objects into Json, Then write the file and save it to the local disk .
This article will use open source libraries ApeFree.DataStore To replace the original object stored procedure , Implement an object storage method that can switch storage methods at will .
About DataStore
ApeFree.DataStore Is a configurable object repository , Support on different platforms / Store and restore objects in memory in media ( Such as local storage 、 Registry storage ). Support configuration serialization format ( Such as Json、Xml), Support the configuration of compression algorithm ( Such as GZip、Defalte), Support the configuration of encryption algorithm ( Such as AES、RSA).
Open source address :https://github.com/landriesnidis/ApeFree.DataStore
Sample code
Entity class
Create an entity type for testing , The initial value is preset ;
/// <summary>
/// Student ( Test entity class )
/// </summary>
public class Student
{
public long Id {
get; set; } = 2022030511;
public string Name {
get; set; } = " Zhang San ";
public DateTime DateOfBirth {
get; set; } = new DateTime(2013, 6, 1);
public string ClassName {
get; set; } = "A class ";
public string Description {
get; set; } = " Ordinary students ";
public bool IsYoungPioneer {
get; set; } = true;
public string Address {
get; set; } = " Erxianqiao Chenghua Avenue 8 Number ";
}
Create object storage
Example 1 、Json Format local memory
// Local storage configuration ( By default Json Format )
var settings = new LoaclStoreAccessSettings("./config/student.conf");
// Local storage
IStore<Student> store = StoreFactory.Factory.CreateLoaclStore<Student>(settings);
Example 2 、Xml Format local memory
// Local storage configuration
var settings = new LoaclStoreAccessSettings("./config/student.conf") {
SerializationAdapter = new XmlSerializationAdapter()
};
// Local storage
IStore<Student> store = StoreFactory.Factory.CreateLoaclStore<Student>(settings);
Example 3 、Xml Format ( use GZip Compression algorithm ) Local storage for
// Local storage configuration
var settings = new LoaclStoreAccessSettings("./config/student.conf") {
SerializationAdapter = new XmlSerializationAdapter(),
CompressionAdapter = new GZipCompressionAdapter(),
};
// Local storage
IStore<Student> store = StoreFactory.Factory.CreateLoaclStore<Student>(settings);
Example 4 、Xml Format ( use AES encryption ) Local storage for
ASE secret key :12345678901234567890123456789012
AES vector :0123456789abcdef
// Local storage configuration
var settings = new LoaclStoreAccessSettings("./config/student.conf") {
SerializationAdapter = new XmlSerializationAdapter(),
EncryptionAdapter = new AesEncryptionAdapter("12345678901234567890123456789012".GetBytes(), "0123456789abcdef".GetBytes()),
};
// Local storage
IStore<Student> store = StoreFactory.Factory.CreateLoaclStore<Student>(settings);
Example 5 、Xml Format (Deflate+AES) Registry memory
Be careful Deflate+AES Just to demonstrate the usage of configuration :
// Registry storage configuration
var settings = new RegistryStoreAccessSettings(RegistryHive.CurrentUser, @"ApeFree\DataStore\Demo","student") {
SerializationAdapter = new XmlSerializationAdapter(),
CompressionAdapter = new DeflateCompressionAdapter(),
EncryptionAdapter = new AesEncryptionAdapter("12345678901234567890123456789012".GetBytes(), "0123456789abcdef".GetBytes()),
};
// Registry memory
IStore<Student> store = StoreFactory.Factory.CreateRegistryStore<Student>(settings);
Test form

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();
// Calculation time ( millisecond )
var elapsedTime = watch.ElapsedTicks * 1000.0 / Stopwatch.Frequency;
MessageBox.Show($" access {
times} The test is over .\r\n" +
$" Total time :{
elapsedTime} millisecond .\r\n" +
$" Average single read + Saving time :{
elapsedTime / times} millisecond ");
}
}
边栏推荐
- 无密码身份验证如何保障用户隐私安全?
- 解决 UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xa2 in position 107
- [cloud native] use of Nacos taskmanager task management
- Hiengine: comparable to the local cloud native memory database engine
- Flutter draws animation effects of wave movement, curves and line graphs
- RHCSA8
- Go array and slice
- Shuttle INKWELL & ink components
- Natural language processing series (I) introduction overview
- go 指针
猜你喜欢

初次使用腾讯云,解决只能使用webshell连接,不能使用ssh连接。

SAE international strategic investment geometry partner

Word document injection (tracking word documents) incomplete

MSTP and eth trunk

A detailed explanation of ASCII code, Unicode and UTF-8

Simple page request and parsing cases

RHCSA8

“百度杯”CTF比赛 九月场,Web:Upload

Solve Unicode decodeerror: 'GBK' codec can't decode byte 0xa2 in position 107

MySQL 巨坑:update 更新慎用影响行数做判断!!!
随机推荐
阿里云SLB负载均衡产品基本概念与购买流程
MySQL splits strings for conditional queries
Small case of function transfer parameters
How to choose note taking software? Comparison and evaluation of notion, flowus and WOLAI
STM32 and motor development (from architecture diagram to documentation)
Binder通信过程及ServiceManager创建过程
MySQL --- 数据库查询 - 排序查询、分页查询
聊聊异步编程的 7 种实现方式
[notes of in-depth study paper]uctransnet: rethink the jumping connection in u-net from the perspective of transformer channel
【每日一题】1200. 最小绝对差
将函数放在模块中
一文详解ASCII码,Unicode与utf-8
Introduction to the principle of DNS
946. 验证栈序列
Go pointer
Overflow toolbar control in SAP ui5 view
It's too convenient. You can complete the code release and approval by nailing it!
go 数组与切片
CloudCompare——点云切片
How to protect user privacy without password authentication?