1
0
mirror of https://github.com/bitwarden/server.git synced 2025-02-23 03:01:23 +01:00

Removed caching

This commit is contained in:
Kyle Spearrin 2016-07-30 16:37:52 -04:00
parent 4a8162d09a
commit f456a4fca8
6 changed files with 12 additions and 65 deletions

View File

@ -18,10 +18,7 @@ using Bit.Core.Repositories;
using Bit.Core.Services;
using Repos = Bit.Core.Repositories.SqlServer;
using System.Text;
using StackExchange.Redis.Extensions.Core;
using StackExchange.Redis.Extensions.Newtonsoft;
using Loggr.Extensions.Logging;
using Newtonsoft.Json;
using System.Linq;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.Net.Http.Headers;
@ -62,16 +59,6 @@ namespace Bit.Api
ConfigurationBinder.Bind(Configuration.GetSection("GlobalSettings"), globalSettings);
services.AddSingleton(s => globalSettings);
// Caching
ISerializer serializer = new NewtonsoftSerializer(new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});
services.AddSingleton(s => serializer);
ICacheClient cacheClient = new StackExchangeRedisCacheClient(serializer,
globalSettings.Cache.ConnectionString, globalSettings.Cache.Database);
services.AddSingleton(s => cacheClient);
// Repositories
services.AddSingleton<IUserRepository, Repos.UserRepository>();
services.AddSingleton<ICipherRepository, Repos.CipherRepository>();

View File

@ -14,10 +14,6 @@
"logKey": "SECRET",
"apiKey": "SECRET"
},
"cache": {
"connectionString": "SECRET.COM:6380,password=SECRET,ssl=True,abortConnect=False",
"database": 0
},
"push": {
"apnsCertificateThumbprint": "SECRET",
"apnsCertificatePassword": "SECRET",

View File

@ -1,7 +0,0 @@
namespace Bit.Core
{
public static class Constants
{
public const string UserIdCacheKey = "User:Id_{0}";
}
}

View File

@ -7,23 +7,18 @@ using DataTableProxy;
using Bit.Core.Domains;
using System.Data;
using Dapper;
using StackExchange.Redis.Extensions.Core;
namespace Bit.Core.Repositories.SqlServer
{
public class CipherRepository : Repository<Cipher, Guid>, ICipherRepository
{
private readonly ICacheClient _cacheClient;
public CipherRepository(GlobalSettings globalSettings, ICacheClient cacheClient)
: this(globalSettings.SqlServer.ConnectionString, cacheClient)
public CipherRepository(GlobalSettings globalSettings)
: this(globalSettings.SqlServer.ConnectionString)
{ }
public CipherRepository(string connectionString, ICacheClient cacheClient)
public CipherRepository(string connectionString)
: base(connectionString)
{
_cacheClient = cacheClient;
}
{ }
public async Task<Cipher> GetByIdAsync(Guid id, Guid userId)
{
@ -87,11 +82,11 @@ namespace Bit.Core.Repositories.SqlServer
}
}
public async Task UpdateUserEmailPasswordAndCiphersAsync(User user, IEnumerable<Cipher> ciphers)
public Task UpdateUserEmailPasswordAndCiphersAsync(User user, IEnumerable<Cipher> ciphers)
{
if(ciphers.Count() == 0)
{
return;
return Task.FromResult(0);
}
using(var connection = new SqlConnection(ConnectionString))
@ -176,8 +171,7 @@ namespace Bit.Core.Repositories.SqlServer
}
}
// Cleanup user cache
await _cacheClient.RemoveAllAsync(new string[] { string.Format(Constants.UserIdCacheKey, user.Id) });
return Task.FromResult(0);
}
public Task CreateAsync(IEnumerable<Cipher> ciphers)

View File

@ -5,37 +5,22 @@ using System.Linq;
using System.Threading.Tasks;
using Bit.Core.Domains;
using Dapper;
using StackExchange.Redis.Extensions.Core;
namespace Bit.Core.Repositories.SqlServer
{
public class UserRepository : Repository<User, Guid>, IUserRepository
{
private readonly ICacheClient _cacheClient;
public UserRepository(GlobalSettings globalSettings, ICacheClient cacheClient)
: this(globalSettings.SqlServer.ConnectionString, cacheClient)
public UserRepository(GlobalSettings globalSettings)
: this(globalSettings.SqlServer.ConnectionString)
{ }
public UserRepository(string connectionString, ICacheClient cacheClient)
public UserRepository(string connectionString)
: base(connectionString)
{
_cacheClient = cacheClient;
}
{ }
public override async Task<User> GetByIdAsync(Guid id)
{
var cacheKey = string.Format(Constants.UserIdCacheKey, id);
var user = await _cacheClient.GetAsync<User>(cacheKey);
if(user != null)
{
return user;
}
user = await base.GetByIdAsync(id);
await _cacheClient.AddAsync(cacheKey, user);
return user;
return await base.GetByIdAsync(id);
}
public async Task<User> GetByEmailAsync(string email)
@ -54,18 +39,11 @@ namespace Bit.Core.Repositories.SqlServer
public override async Task ReplaceAsync(User user)
{
await base.ReplaceAsync(user);
await PurgeCacheAsync(user);
}
public override async Task DeleteAsync(User user)
{
await base.DeleteAsync(user);
await PurgeCacheAsync(user);
}
private async Task PurgeCacheAsync(User user)
{
await _cacheClient.RemoveAllAsync(new string[] { string.Format(Constants.UserIdCacheKey, user.Id) });
}
}
}

View File

@ -12,7 +12,6 @@
"DataTableProxy": "1.2.0",
"Sendgrid": "6.3.4",
"StackExchange.Redis": "1.0.488",
"StackExchange.Redis.Extensions.Newtonsoft": "1.3.5",
"PushSharp": "4.0.10"
},