forked from idg10/prog-cs-8-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserCache.cs
39 lines (34 loc) · 989 Bytes
/
UserCache.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
using System;
using System.Collections.Generic;
using System.Text;
namespace Dictionaries
{
public class UserCache
{
private readonly Dictionary<string, UserInfo> _cachedUserInfo =
new Dictionary<string, UserInfo>();
public UserInfo GetInfo(string userHandle)
{
RemoveStaleCacheEntries();
if (!_cachedUserInfo.TryGetValue(userHandle, out UserInfo info))
{
info = FetchUserInfo(userHandle);
_cachedUserInfo.Add(userHandle, info);
}
return info;
}
private UserInfo FetchUserInfo(string userHandle)
{
// fetch info ...
return new UserInfo();
}
private void RemoveStaleCacheEntries()
{
// application-specific logic deciding when to remove old entries ...
}
}
public class UserInfo
{
// application-specific user information ...
}
}