-
-
Notifications
You must be signed in to change notification settings - Fork 323
/
RedisDataProvider.cs
86 lines (78 loc) · 3.23 KB
/
RedisDataProvider.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
using System.Threading;
using System.Threading.Tasks;
using Audit.Core;
namespace Audit.Redis.Providers
{
/// <summary>
/// Store Audit logs in a Redis database as a strings, lists, hashes, or sortedsets.
/// </summary>
/// <remarks>
/// Settings:
/// Handler: The redis data handler (string, list, hash, sortedset, pubsub)
/// </remarks>
public class RedisDataProvider : AuditDataProvider
{
private readonly RedisProviderHandler _handler;
public RedisDataProvider(RedisProviderHandler handler)
{
_handler = handler;
}
/// <summary>
/// Stores an event in a redis database
/// </summary>
/// <param name="auditEvent">The audit event being created.</param>
public override object InsertEvent(AuditEvent auditEvent)
{
return _handler.Insert(auditEvent);
}
/// <summary>
/// Stores/Updates an event in a redis database, related to a previous event
/// </summary>
/// <param name="auditEvent">The audit event.</param>
/// <param name="eventId">The event id being replaced.</param>
public override void ReplaceEvent(object eventId, AuditEvent auditEvent)
{
var key = _handler.GetKey(auditEvent);
_handler.Replace(key, eventId, auditEvent);
}
/// <summary>
/// Gets an audit event from a redis database
/// </summary>
/// <param name="eventId">The event id</param>
public override T GetEvent<T>(object eventId)
{
var key = _handler.GetKey(null);
return _handler.Get<T>(key, eventId);
}
/// <summary>
/// Stores an event in a redis database asynchronously
/// </summary>
/// <param name="auditEvent">The audit event being created.</param>
/// <param name="cancellationToken">The Cancellation Token.</param>
public override async Task<object> InsertEventAsync(AuditEvent auditEvent, CancellationToken cancellationToken = default)
{
return await _handler.InsertAsync(auditEvent);
}
/// <summary>
/// Stores/Updates an event in a redis database asynchronously, related to a previous event
/// </summary>
/// <param name="auditEvent">The audit event.</param>
/// <param name="eventId">The event id being replaced.</param>
/// <param name="cancellationToken">The Cancellation Token.</param>
public override async Task ReplaceEventAsync(object eventId, AuditEvent auditEvent, CancellationToken cancellationToken = default)
{
var key = _handler.GetKey(auditEvent);
await _handler.ReplaceAsync(key, eventId, auditEvent);
}
/// <summary>
/// Gets an audit event from a redis database asynchronously
/// </summary>
/// <param name="eventId">The event id</param>
/// <param name="cancellationToken">The Cancellation Token.</param>
public override async Task<T> GetEventAsync<T>(object eventId, CancellationToken cancellationToken = default)
{
var key = _handler.GetKey(null);
return await _handler.GetAsync<T>(key, eventId);
}
}
}