当前位置:网站首页>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 ");
}
}
边栏推荐
- Flutter draws animation effects of wave movement, curves and line graphs
- 国际自动机工程师学会(SAE International)战略投资几何伙伴
- [notes of in-depth study paper]uctransnet: rethink the jumping connection in u-net from the perspective of transformer channel
- Introduction to sap ui5 flexiblecolumnlayout control
- 百度杯”CTF比赛 2017 二月场,Web:爆破-2
- RHCSA9
- 爱可生SQLe审核工具顺利完成信通院‘SQL质量管理平台分级能力’评测
- Developers, is cloud native database the future?
- 程序员成长第八篇:做好测试工作
- 155. 最小栈
猜你喜欢
Datapipeline was selected into the 2022 digital intelligence atlas and database development report of China Academy of communications and communications
About the single step debugging of whether SAP ui5 floating footer is displayed or not and the benefits of using SAP ui5
Fragmented knowledge management tool memos
How to realize batch sending when fishing
Shu tianmeng map × Weiyan technology - Dream map database circle of friends + 1
Association modeling method in SAP segw transaction code
Hundred days to complete the open source task of the domestic database opengauss -- openguass minimalist version 3.0.0 installation tutorial
简单上手的页面请求和解析案例
Talk about seven ways to realize asynchronous programming
go 数组与切片
随机推荐
Reflection and imagination on the notation like tool
MySQL giant pit: update updates should be judged with caution by affecting the number of rows!!!
Principle and performance analysis of lepton lossless compression
#从源头解决# 自定义头文件在VS上出现“无法打开源文件“XX.h“的问题
Pycharm installation third party library diagram
前缀、中缀、后缀表达式「建议收藏」
It's too convenient. You can complete the code release and approval by nailing it!
[notes of in-depth study paper]uctransnet: rethink the jumping connection in u-net from the perspective of transformer channel
Navigation property and entityset usage in SAP segw transaction code
使用Dom4j解析XML
聊聊异步编程的 7 种实现方式
爱可生SQLe审核工具顺利完成信通院‘SQL质量管理平台分级能力’评测
Talking about fake demand from takeout order
uni-app开发语音识别app,讲究的就是简单快速。
解决uni-app配置页面、tabBar无效问题
Rocky basics 1
C# 对象存储
Shuttle INKWELL & ink components
PyCharm安装第三方库图解
从外卖点单浅谈伪需求