当前位置:网站首页>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 ");
}
}
边栏推荐
- Reverse Polish notation
- 先写API文档还是先写代码?
- SAE international strategic investment geometry partner
- Talking about fake demand from takeout order
- Put functions in modules
- Principle and performance analysis of lepton lossless compression
- Introduction to the principle of DNS
- 前缀、中缀、后缀表达式「建议收藏」
- 聊聊异步编程的 7 种实现方式
- 函数传递参数小案例
猜你喜欢
Leetcode20. Valid parentheses
Developers, is cloud native database the future?
Solve Unicode decodeerror: 'GBK' codec can't decode byte 0xa2 in position 107
Shandong University Summer Training - 20220620
山东大学暑期实训一20220620
Sorry, we can't open xxxxx Docx, because there is a problem with the content (repackaging problem)
解决uni-app配置页面、tabBar无效问题
Concurrent performance test of SAP Spartacus with JMeter
数据湖(七):Iceberg概念及回顾什么是数据湖
Asemi rectifier bridge hd06 parameters, hd06 pictures, hd06 applications
随机推荐
LB10S-ASEMI整流桥LB10S
RHCSA9
从外卖点单浅谈伪需求
Discussion on error messages and API versions of SAP ui5 getsaplogonlanguage is not a function
Go string operation
APICloud Studio3 WiFi真机同步和WiFi真机预览使用说明
Cf:a. the third three number problem
Although the volume and price fall, why are the structural deposits of commercial banks favored by listed companies?
go 指针
"Baidu Cup" CTF competition in September, web:sql
Actual combat simulation │ JWT login authentication
CloudCompare——点云切片
Rocky basics 1
爱可生SQLe审核工具顺利完成信通院‘SQL质量管理平台分级能力’评测
Introduction to sap ui5 dynamicpage control
Natural language processing series (I) introduction overview
Pycharm installation third party library diagram
Halcon template matching actual code (I)
Hiengine: comparable to the local cloud native memory database engine
JPA规范总结和整理