Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Local Cache Library C# (persistent and encrypted)

I'm looking to add caching to a Windows desktop application written in C# 4.

My requirements are for a persistent key-value store that saves to an encrypted file. I am trying to cache remote calls to a server that are potentially slow—and may cache a considerable amount of data, i.e. 50+ MB. I would like to be able to set a max-file-size value with some form of LRU expiration.

I had thought of serialising a dictionary, but this is likely to be a little slow to initialise, and will have too large a memory footprint.

I'm thinking an encrypted SQLCE 4 database might be the best solution, but this seems heavyweight when I really just want a single hash table. It also doesn't natively offer the caching type features of expiration etc..

Can anyone suggest anything else worth considering, or some suggestion around optimising the serialisation/deserialisation.

like image 645
Chris Avatar asked Nov 05 '22 04:11

Chris


2 Answers

You should absolutely consider serializing. I went round and round with this (including trying SQLCE) a few months ago and there was nothing close to the speed of serialized objects (in my case I was using custom objects) - SQLCE took 2-3 times as long to load as serialized objects.

If the memory footprint is too big, then you should reconsider how you are designing what you are persisting.

like image 136
John Avatar answered Nov 09 '22 12:11

John


I would advise using SQLite over SQLCE 4. The SQLlite engine is explicitly designed to work in memory constrained environments. The decision of memory vs speed is left to the developer.

The open source ADO.NET library allows you to take full advantage of the Microsoft database tools (DbProviderFactory, Entity Framework, Server Explorer, etc). It also natively supports encryption.

The LRU implementation would be pretty straightforward given a schema like the following:

CREATE TABLE "cache" ("key" varchar(64) NOT NULL PRIMARY KEY, "value" text NOT NULL, "last_access" datetime NOT NULL);
CREATE INDEX "cache_lru" ON "cache" ("last_access");
like image 28
Erik Karulf Avatar answered Nov 09 '22 12:11

Erik Karulf