当前位置:网站首页>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 ");
}
}
边栏推荐
- Leetcode20. Valid parentheses
- 简单上手的页面请求和解析案例
- 山东大学暑期实训一20220620
- Sorry, we can't open xxxxx Docx, because there is a problem with the content (repackaging problem)
- Reverse Polish notation
- APICloud Studio3 WiFi真机同步和WiFi真机预览使用说明
- Shandong University Summer Training - 20220620
- 百度杯”CTF比赛 2017 二月场,Web:爆破-2
- Shi Zhenzhen's 2021 summary and 2022 outlook | colorful eggs at the end of the article
- leetcode:221. Maximum square [essence of DP state transition]
猜你喜欢

PyCharm安装第三方库图解

uni-app开发语音识别app,讲究的就是简单快速。

LeetCode20.有效的括号

I'm doing open source in Didi

Principle and performance analysis of lepton lossless compression

Introduction to the principle of DNS

Although the volume and price fall, why are the structural deposits of commercial banks favored by listed companies?

How can non-technical departments participate in Devops?

Le rapport de recherche sur l'analyse matricielle de la Force des fournisseurs de RPA dans le secteur bancaire chinois en 2022 a été officiellement lancé.

SAP ui5 objectpagelayout control usage sharing
随机推荐
A detailed explanation of ASCII code, Unicode and UTF-8
Hiengine: comparable to the local cloud native memory database engine
I'm doing open source in Didi
FPGA 学习笔记:Vivado 2019.1 添加 IP MicroBlaze
155. Minimum stack
量价虽降,商业银行结构性存款为何受上市公司所偏爱?
Shu tianmeng map × Weiyan technology - Dream map database circle of friends + 1
STM32 and motor development (from architecture diagram to documentation)
Discussion on error messages and API versions of SAP ui5 getsaplogonlanguage is not a function
js判断数组中是否存在某个元素(四种方法)
MySQL 巨坑:update 更新慎用影响行数做判断!!!
Get to know linkerd project for the first time
Difference between avc1 and H264
程序员成长第八篇:做好测试工作
ASEMI整流桥HD06参数,HD06图片,HD06应用
Hundred days to complete the open source task of the domestic database opengauss -- openguass minimalist version 3.0.0 installation tutorial
爱可生SQLe审核工具顺利完成信通院‘SQL质量管理平台分级能力’评测
Flutter InkWell & Ink组件
Introduction to sap ui5 dynamicpage control
从外卖点单浅谈伪需求