-
Notifications
You must be signed in to change notification settings - Fork 111
/
IdentityRepository.cs
43 lines (37 loc) · 1.29 KB
/
IdentityRepository.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
using System;
using System.Threading.Tasks;
using Identity.Api.Models;
using Newtonsoft.Json;
using StackExchange.Redis;
namespace Identity.Api.Services
{
public class IdentityRepository : IIdentityRepository
{
private readonly IDatabase _database;
public IdentityRepository(ConnectionMultiplexer redis)
{
_database = redis.GetDatabase();
}
public async Task<User> UpdateUserAsync(User user)
{
var created = await _database.StringSetAsync(user.Id, JsonConvert.SerializeObject(user));
if (!created) return null;
return await GetUserAsync(user.Id);
}
public async Task<long> UpdateUserApplicationCountAsync(string userId)
{
return await _database.StringIncrementAsync($"{userId}-appcnt");
}
public async Task<User> GetUserAsync(string userId)
{
var data = await _database.StringGetAsync(userId);
return data.IsNullOrEmpty ? null : JsonConvert.DeserializeObject<User>(data);
}
public async Task<long> GetUserApplicationCountAsync(string userId)
{
var data = await _database.StringGetAsync($"{userId}-appcnt");
return data.IsNullOrEmpty ? 0 : Convert.ToInt64(data);
}
}
}