-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocalStorage.cs
79 lines (70 loc) · 2.14 KB
/
LocalStorage.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using SQLite;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace TestConsole
{
//LocalStorage Synchronous Sqlite implementation
//https://developer.mozilla.org/es/docs/Web/API/Storage/setItem
public class LocalStorage
{
public SQLiteConnection connection;
public string path;
private static string dbName = "LocalStorage.db";
//Default Constructor with LocalApplicationData Folder
public LocalStorage()
: this(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), dbName))
{
}
public LocalStorage(string path )
{
Console.WriteLine("Hello World!");
this.path = path;
var databasePath = path;
connection = new SQLiteConnection(databasePath);
connection.CreateTable<KeyValue>();
}
public void setItem(string key, string data)
{
connection.InsertOrReplace(new KeyValue() { key = key, value = data });
}
public string getItem(string key)
{
string result = "";
try
{
result = connection.Find<KeyValue>(key).value;
}
catch(Exception e)
{
return "";
}
return result;
}
public void removeItem(string key)
{
connection.Delete<KeyValue>(key);
}
public void clear(object p)
{
connection.DeleteAll<KeyValue>();
}
public object key(int n)
{
var list = connection.Table<KeyValue>().ToList();
return list[n].key;
}
}
class KeyValue
{
[PrimaryKey]
public string key { get; set; }
public string value { get; set; }
}
//TODO: check if change for another implementation like:
//https://github.com/Microsoft/FASTER
//Add a checkpoint functionality.
//TODO: do i need to chang it to async methods to avoid freeze the thread ?
//TODO: use and id property on KeyValue for key() method
}