parent
50e9225574
commit
3fa605177c
@ -0,0 +1,23 @@
|
|||||||
|
using Dapper;
|
||||||
|
using NzbDrone.Core.Datastore;
|
||||||
|
|
||||||
|
namespace NzbDrone.Core.Housekeeping.Housekeepers
|
||||||
|
{
|
||||||
|
public class TrimHttpCache : IHousekeepingTask
|
||||||
|
{
|
||||||
|
private readonly ICacheDatabase _database;
|
||||||
|
|
||||||
|
public TrimHttpCache(ICacheDatabase database)
|
||||||
|
{
|
||||||
|
_database = database;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Clean()
|
||||||
|
{
|
||||||
|
using (var mapper = _database.OpenConnection())
|
||||||
|
{
|
||||||
|
mapper.Execute(@"DELETE FROM HttpResponse WHERE Expiry < date('now')");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
using System;
|
||||||
|
using NzbDrone.Core.Datastore;
|
||||||
|
|
||||||
|
namespace NzbDrone.Core.Http
|
||||||
|
{
|
||||||
|
public class CachedHttpResponse : ModelBase
|
||||||
|
{
|
||||||
|
public string Url { get; set; }
|
||||||
|
public DateTime LastRefresh { get; set; }
|
||||||
|
public DateTime Expiry { get; set; }
|
||||||
|
public string Value { get; set; }
|
||||||
|
public int StatusCode { get; set; }
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
using System.Linq;
|
||||||
|
using NzbDrone.Core.Datastore;
|
||||||
|
using NzbDrone.Core.Messaging.Events;
|
||||||
|
|
||||||
|
namespace NzbDrone.Core.Http
|
||||||
|
{
|
||||||
|
public interface ICachedHttpResponseRepository : IBasicRepository<CachedHttpResponse>
|
||||||
|
{
|
||||||
|
CachedHttpResponse FindByUrl(string url);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class CachedHttpResponseRepository : BasicRepository<CachedHttpResponse>, ICachedHttpResponseRepository
|
||||||
|
{
|
||||||
|
public CachedHttpResponseRepository(ICacheDatabase database,
|
||||||
|
IEventAggregator eventAggregator)
|
||||||
|
: base(database, eventAggregator)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public CachedHttpResponse FindByUrl(string url)
|
||||||
|
{
|
||||||
|
var edition = Query(x => x.Url == url).SingleOrDefault();
|
||||||
|
|
||||||
|
return edition;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,58 @@
|
|||||||
|
using System;
|
||||||
|
using System.Net;
|
||||||
|
using NzbDrone.Common.Http;
|
||||||
|
|
||||||
|
namespace NzbDrone.Core.Http
|
||||||
|
{
|
||||||
|
public interface ICachedHttpResponseService
|
||||||
|
{
|
||||||
|
HttpResponse Get(HttpRequest request, TimeSpan ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class CachedHttpResponseService : ICachedHttpResponseService
|
||||||
|
{
|
||||||
|
private readonly ICachedHttpResponseRepository _repo;
|
||||||
|
private readonly IHttpClient _httpClient;
|
||||||
|
|
||||||
|
public CachedHttpResponseService(ICachedHttpResponseRepository httpResponseRepository,
|
||||||
|
IHttpClient httpClient)
|
||||||
|
{
|
||||||
|
_repo = httpResponseRepository;
|
||||||
|
_httpClient = httpClient;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpResponse Get(HttpRequest request, TimeSpan ttl)
|
||||||
|
{
|
||||||
|
var cached = _repo.FindByUrl(request.Url.ToString());
|
||||||
|
|
||||||
|
if (cached != null && cached.Expiry > DateTime.UtcNow)
|
||||||
|
{
|
||||||
|
return new HttpResponse(request, new HttpHeader(), cached.Value, (HttpStatusCode)cached.StatusCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = _httpClient.Get(request);
|
||||||
|
|
||||||
|
if (!result.HasHttpError)
|
||||||
|
{
|
||||||
|
if (cached == null)
|
||||||
|
{
|
||||||
|
cached = new CachedHttpResponse
|
||||||
|
{
|
||||||
|
Url = request.Url.ToString(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
var now = DateTime.UtcNow;
|
||||||
|
|
||||||
|
cached.LastRefresh = now;
|
||||||
|
cached.Expiry = now.Add(ttl);
|
||||||
|
cached.Value = result.Content;
|
||||||
|
cached.StatusCode = (int)result.StatusCode;
|
||||||
|
|
||||||
|
_repo.Upsert(cached);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue