Skip to content

Commit

Permalink
Limit the round-trips to the database
Browse files Browse the repository at this point in the history
We only need to fetch ddo items by token once.
Tokens are unique so we can easily store them in synchronized
cache.

Fixes: #235
  • Loading branch information
barteksekula committed Sep 1, 2023
1 parent 07b3c08 commit bf3a1eb
Showing 1 changed file with 15 additions and 3 deletions.
Expand Up @@ -4,6 +4,7 @@
using Advanced.CMS.ExternalReviews.PinCodeSecurity;
using EPiServer.Core;
using EPiServer.Data.Dynamic;
using EPiServer.Framework.Cache;
using EPiServer.ServiceLocation;

namespace Advanced.CMS.ExternalReviews.ReviewLinksRepository
Expand Down Expand Up @@ -34,12 +35,14 @@ public class ExternalReviewLinksRepository: IExternalReviewLinksRepository
{
private readonly ExternalReviewLinkBuilder _externalReviewLinkBuilder;
private readonly DynamicDataStoreFactory _dataStoreFactory;
private readonly ISynchronizedObjectInstanceCache _cache;

public ExternalReviewLinksRepository(ExternalReviewLinkBuilder externalReviewLinkBuilder,
DynamicDataStoreFactory dataStoreFactory)
DynamicDataStoreFactory dataStoreFactory, ISynchronizedObjectInstanceCache cache)
{
_externalReviewLinkBuilder = externalReviewLinkBuilder;
_dataStoreFactory = dataStoreFactory;
_cache = cache;
}

public IEnumerable<ExternalReviewLink> GetLinksForContent(ContentReference contentLink, int? projectId)
Expand Down Expand Up @@ -74,7 +77,15 @@ private ExternalReviewLinkDds GetReviewLinkDds(string token)
}

token = token.ToLower();
return GetStore().Items<ExternalReviewLinkDds>().FirstOrDefault(x => x.Token == token);
if (_cache.Get(token) is ExternalReviewLinkDds item)
{
return item;
}

var externalReviewLinkDds = GetStore().Items<ExternalReviewLinkDds>().FirstOrDefault(x => x.Token == token);
_cache.Insert(token, externalReviewLinkDds, CacheEvictionPolicy.Empty);

return externalReviewLinkDds;
}

public bool HasPinCode(string token)
Expand Down Expand Up @@ -140,7 +151,7 @@ public ExternalReviewLink UpdateLink(string token, DateTime? validTo, string pin
item.VisitorGroups = visitorGroups ?? new[] {"cc5fc022-4186-431e-b38a-e257d8cafd51"};

store.Save(item);

_cache.Remove(token);
return _externalReviewLinkBuilder.FromExternalReview(item);
}

Expand All @@ -153,6 +164,7 @@ public void DeleteLink(string token)
return;
}
GetStore().Delete(item.Id);
_cache.Remove(token);
}

private DynamicDataStore GetStore()
Expand Down

0 comments on commit bf3a1eb

Please sign in to comment.