当前位置:网站首页>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 ");
}
}
边栏推荐
- A small talk caused by the increase of sweeping
- 数据湖(七):Iceberg概念及回顾什么是数据湖
- 爱可生SQLe审核工具顺利完成信通院‘SQL质量管理平台分级能力’评测
- Solve Unicode decodeerror: 'GBK' codec can't decode byte 0xa2 in position 107
- 山东大学暑期实训一20220620
- Hundred days to complete the open source task of the domestic database opengauss -- openguass minimalist version 3.0.0 installation tutorial
- 程序员成长第八篇:做好测试工作
- RHCSA9
- Flutter InkWell & Ink组件
- A detailed explanation of ASCII code, Unicode and UTF-8
猜你喜欢

Word document injection (tracking word documents) incomplete

峰会回顾|保旺达-合规和安全双驱动的数据安全整体防护体系

CloudCompare——点云切片

蜀天梦图×微言科技丨达梦图数据库朋友圈+1

How to protect user privacy without password authentication?

Natural language processing series (I) introduction overview

How to realize batch sending when fishing

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

LeetCode20.有效的括号

MySQL --- 数据库查询 - 排序查询、分页查询
随机推荐
leetcode:221. 最大正方形【dp状态转移的精髓】
Hiengine: comparable to the local cloud native memory database engine
How to realize batch sending when fishing
JXL notes
Hiengine: comparable to the local cloud native memory database engine
Talking about fake demand from takeout order
阿里云SLB负载均衡产品基本概念与购买流程
RHCSA9
Natural language processing series (I) introduction overview
Principle and configuration of RSTP protocol
DataPipeline双料入选中国信通院2022数智化图谱、数据库发展报告
Halcon template matching actual code (I)
Pandora IOT development board learning (HAL Library) - Experiment 7 window watchdog experiment (learning notes)
【每日一题】1200. 最小绝对差
APICloud Studio3 API管理与调试使用教程
Shandong University Summer Training - 20220620
The solution of outputting 64 bits from printf format%lld of cross platform (32bit and 64bit)
AVC1与H264的区别
初次使用腾讯云,解决只能使用webshell连接,不能使用ssh连接。
A small talk caused by the increase of sweeping