This repository has been archived by the owner on Jun 21, 2023. It is now read-only.
/
CacheIndex.cs
99 lines (86 loc) · 3.51 KB
/
CacheIndex.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
95
96
97
98
99
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reactive.Linq;
using Akavache;
using GitHub.Extensions;
namespace GitHub.Caches
{
public class CacheIndex
{
public const string PRPrefix = "index:pr";
public const string RepoPrefix = "index:repos";
public const string GitIgnoresPrefix = "index:ignores";
public const string LicensesPrefix = "index:licenses";
public const string FileContentsPrefix = "index:filecontents";
public static CacheIndex Create(string key)
{
return new CacheIndex { IndexKey = key };
}
public CacheIndex()
{
Keys = new List<string>();
OldKeys = new List<string>();
}
public CacheIndex Add(string indexKey, CacheItem item)
{
Guard.ArgumentNotEmptyString(indexKey, nameof(indexKey));
Guard.ArgumentNotNull(item, nameof(item));
var k = string.Format(CultureInfo.InvariantCulture, "{0}|{1}", IndexKey, item.Key);
if (!Keys.Contains(k))
Keys.Add(k);
UpdatedAt = DateTimeOffset.UtcNow;
return this;
}
public IObservable<CacheIndex> AddAndSave(IBlobCache cache, string indexKey, CacheItem item,
DateTimeOffset? absoluteExpiration = null)
{
Guard.ArgumentNotNull(cache, nameof(cache));
Guard.ArgumentNotEmptyString(indexKey, nameof(indexKey));
Guard.ArgumentNotNull(item, nameof(item));
var k = string.Format(CultureInfo.InvariantCulture, "{0}|{1}", IndexKey, item.Key);
if (!Keys.Contains(k))
Keys.Add(k);
UpdatedAt = DateTimeOffset.UtcNow;
return cache.InsertObject(IndexKey, this, absoluteExpiration)
.Select(x => this);
}
public static IObservable<CacheIndex> AddAndSaveToIndex(IBlobCache cache, string indexKey, CacheItem item,
DateTimeOffset? absoluteExpiration = null)
{
Guard.ArgumentNotNull(cache, nameof(cache));
Guard.ArgumentNotEmptyString(indexKey, nameof(indexKey));
Guard.ArgumentNotNull(item, nameof(item));
return cache.GetOrCreateObject(indexKey, () => Create(indexKey))
.Select(x => x.IndexKey == null ? Create(indexKey) : x)
.Do(index =>
{
var k = string.Format(CultureInfo.InvariantCulture, "{0}|{1}", index.IndexKey, item.Key);
if (!index.Keys.Contains(k))
index.Keys.Add(k);
index.UpdatedAt = DateTimeOffset.UtcNow;
})
.SelectMany(index => cache.InsertObject(index.IndexKey, index, absoluteExpiration)
.Select(x => index));
}
public CacheIndex Clear()
{
OldKeys = Keys.ToList();
Keys.Clear();
UpdatedAt = DateTimeOffset.UtcNow;
return this;
}
public IObservable<CacheIndex> Save(IBlobCache cache,
DateTimeOffset? absoluteExpiration = null)
{
Guard.ArgumentNotNull(cache, nameof(cache));
return cache.InsertObject(IndexKey, this, absoluteExpiration)
.Select(x => this);
}
public string IndexKey { get; set; }
public List<string> Keys { get; set; }
public DateTimeOffset UpdatedAt { get; set; }
public List<string> OldKeys { get; set; }
}
}