-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUserService.cs
More file actions
67 lines (60 loc) · 2.04 KB
/
Copy pathUserService.cs
File metadata and controls
67 lines (60 loc) · 2.04 KB
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
using Ao.Cache.Annotations;
using LiteDB;
namespace Ao.Cache.Sample.CodeGen
{
[CacheProxy(ProxyAll = false)]
public class UserService
{
public UserService(ICacheHelperCreator creator, ILiteDatabase database)
{
CacheCreator = creator;
Database = database;
Users = database.GetCollection<User>(BsonAutoId.Int64);
Users.EnsureIndex(x => x.Name);
}
public ICacheHelperCreator CacheCreator { get; }
public ILiteDatabase Database { get; }
public ILiteCollection<User> Users { get; }
[CacheProxyMethod(CacheTime = "00:01:00")]
public virtual User[] AllUser()
{
Console.WriteLine("Run in raw method");
return Users.FindAll().ToArray();
}
[CacheProxyMethod(CacheTime = "00:01:00")]
public virtual User? GetUser(string name)
{
Console.WriteLine("Run in raw method");
return Users.Find(x => x.Name == name).FirstOrDefault();
}
public virtual BsonValue Add(string name)
{
var res = Users.Insert(new User { Name = name, Number = Random.Shared.Next(0, 9999) });
CacheCreator.Delete(() => AllUser());
return res;
}
public virtual int AddRange(int count)
{
var res = Users.Insert(Enumerable.Range(0, count).Select(x => new User
{
Name = Random.Shared.Next(0, 99999).ToString(),
Number = Random.Shared.Next(0, 9999)
}));
CacheCreator.Delete(() => AllUser());
return res;
}
public virtual BsonValue Delete(string name)
{
var res = Users.DeleteMany(x => x.Name == name);
CacheCreator.Delete(() => AllUser());
CacheCreator.Delete(() => GetUser(name));
return res;
}
public int Clear()
{
var res = Users.DeleteAll();
CacheCreator.Delete(() => AllUser());
return res;
}
}
}