/
CachingSynchronizationAgent.cs
71 lines (61 loc) · 1.76 KB
/
CachingSynchronizationAgent.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
using System.Collections.Generic;
using System.Linq;
namespace WhiskWork.Core.Synchronization
{
public class CachingSynchronizationAgent : ISynchronizationAgent
{
private readonly ISynchronizationAgent _innerAgent;
private IList<SynchronizationEntry> _getAllCache;
public CachingSynchronizationAgent(ISynchronizationAgent agent)
{
_innerAgent = agent;
}
public IEnumerable<SynchronizationEntry> GetAll()
{
if(_getAllCache==null)
{
_getAllCache = _innerAgent.GetAll().ToList();
}
return _getAllCache;
}
public void Create(SynchronizationEntry entry)
{
_innerAgent.Create(entry);
if(_getAllCache!=null)
{
_getAllCache.Add(entry);
}
}
public void Delete(SynchronizationEntry entry)
{
_innerAgent.Delete(entry);
if (_getAllCache != null)
{
_getAllCache.Remove(entry);
}
}
public void UpdateStatus(SynchronizationEntry entry)
{
_innerAgent.UpdateStatus(entry);
var index = IndexOf(entry.Id);
_getAllCache[index] = entry;
}
public void UpdateData(SynchronizationEntry entry)
{
_innerAgent.UpdateData(entry);
var index = IndexOf(entry.Id);
_getAllCache[index] = entry;
}
private int IndexOf(string id)
{
for (var i = 0; i < _getAllCache.Count;i++ )
{
if(_getAllCache[i].Id==id)
{
return i;
}
}
return -1;
}
}
}