-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCache.cs
94 lines (79 loc) · 2.5 KB
/
Cache.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System.Linq.Expressions;
using System.Reflection;
using LiteDB;
// ReSharper disable UnusedType.Global
// ReSharper disable UnusedMember.Global
namespace LocalObjectCache;
public class Cache : IDisposable
{
private readonly string _fileName;
private readonly TimeSpan _cacheValidity;
private readonly LiteDatabase _db;
public Cache(string fileName, TimeSpan cacheValidity)
{
_fileName = fileName;
_cacheValidity = cacheValidity;
_db = new LiteDatabase(_fileName);
_db.Checkpoint();
_db.Rebuild();
}
public void Dispose()
{
if (_db != null)
{
_db.Dispose();
}
}
#region Default Static
/// <summary>
/// local object cache with default settings
/// </summary>
public static readonly Cache Default = new("cache.db", TimeSpan.FromDays(1));
#endregion
private ILiteCollection<T> GetCollection<T>()
{
var type = typeof(T);
var collectionName = Pluralize(type.Name);
var collection = _db.GetCollection<T>(collectionName);
foreach (var propertyInfo in type.GetProperties())
{
var attr = propertyInfo.GetCustomAttribute<IndexAttribute>();
if (attr != null)
{
collection.EnsureIndex(propertyInfo.Name, attr.IsUnique);
}
}
return collection;
}
private string Pluralize(string singleName)
{
if (singleName.EndsWith('y'))
{
return singleName.Substring(0, singleName.Length - 1) + "ies";
}
return singleName + "s";
}
public T GetOne<T>(Expression<Func<T, bool>> predicate)
{
var collection = GetCollection<T>();
var documents = collection.Query().Where(predicate).ToDocuments();
var document = documents.FirstOrDefault();
return collection.EnsureDocumentValidity<T>(document, _cacheValidity);
}
public List<T> GetMany<T>(Expression<Func<T, bool>> predicate)
{
var collection = GetCollection<T>();
var documents = collection.Query().Where(predicate).ToDocuments();
return documents.Select(d => collection.EnsureDocumentValidity<T>(d, _cacheValidity)).ToList();
}
public void InsertOne<T>(T item)
{
var collection = GetCollection<T>();
collection.Insert(item);
}
public void InsertMany<T>(IEnumerable<T> items)
{
var collection = GetCollection<T>();
collection.InsertBulk(items);
}
}