LocalStorage 2.0.0-beta-2

A simple and lightweight tool for persisting data in dotnet (core) apps.

This is a prerelease version of LocalStorage.
There is a newer version of this package available.
See the version list below for details.
Install-Package LocalStorage -Version 2.0.0-beta-2
dotnet add package LocalStorage --version 2.0.0-beta-2
<PackageReference Include="LocalStorage" Version="2.0.0-beta-2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add LocalStorage --version 2.0.0-beta-2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: LocalStorage, 2.0.0-beta-2"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install LocalStorage as a Cake Addin
#addin nuget:?package=LocalStorage&version=2.0.0-beta-2&prerelease

// Install LocalStorage as a Cake Tool
#tool nuget:?package=LocalStorage&version=2.0.0-beta-2&prerelease
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

LocalStorage for .NET

What is LocalStorage?

LocalStorage is a simple utility that solves a common problem pragmatically - storing and accessing objects quickly in a .NET app. It is designed with a specific vision in mind: provide a simple solution for persisting objects, even between sessions, that is unobtrusive and requires zero configuration.

Getting Started

First, you might want to dive into the examples in this document. Want more, have a look at the tests.

Once you're game, simply add it to your project through NuGet.

NuGet Package Manager:

$ Install-Package LocalStorage

NuGet CLI:

$ nuget install LocalStorage

If you've got an issue...

... grab a tissue. No, seriously; don't hesitate to post an issue, or send me a message at @jhanssens.

LocalStorage is Copyright © 2016-2017 Juliën Hanssens under the MIT license.

Documentation

Examples

The tests are some pretty good examples to get familiar with the lib. But here is the short summary on the LocalStorage API.

Basic CRUD operations
Initialize
// initialize, with default settings
var storage = new LocalStorage();

// ... or initialize with a custom configuration 
var config = new LocalStorageConfiguration() { 
	// see the section "Configuration" further on
};

var storage = new LocalStorage(config);
Store()
// store any object, or collection providing only a 'key'
var key = "whatever";
var value = "...";

storage.Store(key, value);
Get()
// fetch any object - as object
storage.Get(key);

// if you know the type, simply provide it as a generic parameter
storage.Get<Animal>(key);
Query()
// fetch a strong-typed collection
storage.Query<Animal>(key);

// you can also provide a strong-typed where-clause in one go
storage.Query<Animal>(key, x => x.Name == "Sloth");
Other operations
Count

Returns the amount of items currently in the LocalStorage container.

Clear()

Clears the in-memory contents of the LocalStorage, but leaves any persisted state on disk intact.

Destroy()

Deletes the persisted file on disk, if it exists, but keeps the in-memory data intact.

Load()

Loads the persisted state from disk into memory, overriding the current memory instance. If the file does not exist, it simply does nothing.
By default, this is done automatically at initialization and can be overriden by disabling AutoLoad in the configuration.

Persist()

Persists the in-memory store to disk.
Note that by default, this is also done automatically when the LocalStorage disposes properly. This can be changed by disabling AutoSave in the configuration.

Configuration

Here is a sample configuration with all configurable members (and their default values assigned):

  • AutoLoad (bool)
    Indicates if LocalStorage should automatically load previously persisted state from disk, when it is initialized (defaults to true).

  • AutoSave (bool)
    Indicates if LocalStorage should automatically persist the latest state to disk, on dispose (defaults to true).

  • Filename (string)
    Filename for the persisted state on disk (defaults to ".localstorage").

  • EnableEncryption

Security is an important feature. LocalStorage has support for encrypting the data, both in-memory as well as persisted on disk.

You only need to define a custom configuration indication that encryption should be enabled:

// setup a configuration with encryption enabled (defaults to 'false')
// note that adding EncryptionSalt is optional, but recommended
var config = new LocalStorageConfiguration() {
	EnableEncryption = true,
	EncryptionSalt = "(optional) add your own random salt string"
};

// initialize LocalStorage with a password of your choice
var encryptedStorage = new LocalStorage(config, "password");

All write operations are first encrypted with AES, before they are persisted in-memory. In case of disk persistance, the encrypted value is respected. Although enabling encryption increases security, it does add a slight overhead to the Get/Store operations, in terms of performance.

By design, only the values are encrypted and not the keys.

LocalStorage for .NET

What is LocalStorage?

LocalStorage is a simple utility that solves a common problem pragmatically - storing and accessing objects quickly in a .NET app. It is designed with a specific vision in mind: provide a simple solution for persisting objects, even between sessions, that is unobtrusive and requires zero configuration.

Getting Started

First, you might want to dive into the examples in this document. Want more, have a look at the tests.

Once you're game, simply add it to your project through NuGet.

NuGet Package Manager:

$ Install-Package LocalStorage

NuGet CLI:

$ nuget install LocalStorage

If you've got an issue...

... grab a tissue. No, seriously; don't hesitate to post an issue, or send me a message at @jhanssens.

LocalStorage is Copyright © 2016-2017 Juliën Hanssens under the MIT license.

Documentation

Examples

The tests are some pretty good examples to get familiar with the lib. But here is the short summary on the LocalStorage API.

Basic CRUD operations
Initialize
// initialize, with default settings
var storage = new LocalStorage();

// ... or initialize with a custom configuration 
var config = new LocalStorageConfiguration() { 
	// see the section "Configuration" further on
};

var storage = new LocalStorage(config);
Store()
// store any object, or collection providing only a 'key'
var key = "whatever";
var value = "...";

storage.Store(key, value);
Get()
// fetch any object - as object
storage.Get(key);

// if you know the type, simply provide it as a generic parameter
storage.Get<Animal>(key);
Query()
// fetch a strong-typed collection
storage.Query<Animal>(key);

// you can also provide a strong-typed where-clause in one go
storage.Query<Animal>(key, x => x.Name == "Sloth");
Other operations
Count

Returns the amount of items currently in the LocalStorage container.

Clear()

Clears the in-memory contents of the LocalStorage, but leaves any persisted state on disk intact.

Destroy()

Deletes the persisted file on disk, if it exists, but keeps the in-memory data intact.

Load()

Loads the persisted state from disk into memory, overriding the current memory instance. If the file does not exist, it simply does nothing.
By default, this is done automatically at initialization and can be overriden by disabling AutoLoad in the configuration.

Persist()

Persists the in-memory store to disk.
Note that by default, this is also done automatically when the LocalStorage disposes properly. This can be changed by disabling AutoSave in the configuration.

Configuration

Here is a sample configuration with all configurable members (and their default values assigned):

  • AutoLoad (bool)
    Indicates if LocalStorage should automatically load previously persisted state from disk, when it is initialized (defaults to true).

  • AutoSave (bool)
    Indicates if LocalStorage should automatically persist the latest state to disk, on dispose (defaults to true).

  • Filename (string)
    Filename for the persisted state on disk (defaults to ".localstorage").

  • EnableEncryption

Security is an important feature. LocalStorage has support for encrypting the data, both in-memory as well as persisted on disk.

You only need to define a custom configuration indication that encryption should be enabled:

// setup a configuration with encryption enabled (defaults to 'false')
// note that adding EncryptionSalt is optional, but recommended
var config = new LocalStorageConfiguration() {
	EnableEncryption = true,
	EncryptionSalt = "(optional) add your own random salt string"
};

// initialize LocalStorage with a password of your choice
var encryptedStorage = new LocalStorage(config, "password");

All write operations are first encrypted with AES, before they are persisted in-memory. In case of disk persistance, the encrypted value is respected. Although enabling encryption increases security, it does add a slight overhead to the Get/Store operations, in terms of performance.

By design, only the values are encrypted and not the keys.

Release Notes

See the releases page on GitHub for release notes:
https://github.com/hanssens/localstorage-for-dotnet/releases

NuGet packages (1)

Showing the top 1 NuGet packages that depend on LocalStorage:

Package Downloads
OnePoint.Flow
OnePoint Global Data FLow Library

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
2.1.0 1,335 2/18/2021
2.0.0 20,105 3/19/2019
2.0.0-beta-2 1,649 8/14/2018
2.0.0-beta 570 8/13/2018
1.1.0 9,772 2/25/2017
1.0.0 805 2/11/2017
1.0.0-preview 774 1/28/2017