From b5abbc7c307f15a12e330f36a20cc1e0983eaf09 Mon Sep 17 00:00:00 2001 From: Shannon Date: Thu, 14 Jan 2016 18:11:48 +0100 Subject: [PATCH] Adds some null checks --- .../Cache/DefaultRepositoryCachePolicy.cs | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/Umbraco.Core/Cache/DefaultRepositoryCachePolicy.cs b/src/Umbraco.Core/Cache/DefaultRepositoryCachePolicy.cs index a5777a5775..58b3a3956e 100644 --- a/src/Umbraco.Core/Cache/DefaultRepositoryCachePolicy.cs +++ b/src/Umbraco.Core/Cache/DefaultRepositoryCachePolicy.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using Umbraco.Core.Logging; using Umbraco.Core.Models.EntityBase; namespace Umbraco.Core.Cache @@ -19,12 +20,17 @@ namespace Umbraco.Core.Cache public DefaultRepositoryCachePolicy(IRuntimeCacheProvider cache, RepositoryCachePolicyOptions options) { + if (cache == null) throw new ArgumentNullException("cache"); + if (options == null) throw new ArgumentNullException("options"); + _options = options; Cache = cache; } public string GetCacheIdKey(object id) { + if (id == null) throw new ArgumentNullException("id"); + return string.Format("{0}{1}", GetCacheTypeKey(), id); } @@ -35,6 +41,9 @@ namespace Umbraco.Core.Cache public void CreateOrUpdate(TEntity entity, Action persistMethod) { + if (entity == null) throw new ArgumentNullException("entity"); + if (persistMethod == null) throw new ArgumentNullException("persistMethod"); + try { persistMethod(entity); @@ -72,6 +81,9 @@ namespace Umbraco.Core.Cache public void Remove(TEntity entity, Action persistMethod) { + if (entity == null) throw new ArgumentNullException("entity"); + if (persistMethod == null) throw new ArgumentNullException("persistMethod"); + persistMethod(entity); //set the disposal action @@ -86,6 +98,8 @@ namespace Umbraco.Core.Cache public TEntity Get(TId id, Func getFromRepo) { + if (getFromRepo == null) throw new ArgumentNullException("getFromRepo"); + var cacheKey = GetCacheIdKey(id); var fromCache = Cache.GetCacheItem(cacheKey); if (fromCache != null) @@ -107,6 +121,8 @@ namespace Umbraco.Core.Cache public bool Exists(TId id, Func getFromRepo) { + if (getFromRepo == null) throw new ArgumentNullException("getFromRepo"); + var cacheKey = GetCacheIdKey(id); var fromCache = Cache.GetCacheItem(cacheKey); return fromCache != null || getFromRepo(id); @@ -114,6 +130,8 @@ namespace Umbraco.Core.Cache public virtual TEntity[] GetAll(TId[] ids, Func> getFromRepo) { + if (getFromRepo == null) throw new ArgumentNullException("getFromRepo"); + if (ids.Any()) { var entities = ids.Select(Get).ToArray(); @@ -191,6 +209,8 @@ namespace Umbraco.Core.Cache /// protected virtual void SetCacheAction(string cacheKey, TEntity entity) { + if (entity == null) return; + SetCacheAction(() => { //just to be safe, we cannot cache an item without an identity