Skip to content

Commit ac7cb8f

Browse files
committed
Fixes cache providers to ensure it stores and resolves clones
1 parent 6e166bd commit ac7cb8f

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

src/Umbraco.Core/Persistence/Caching/InMemoryCacheProvider.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ public IEntity GetById(Type type, Guid id)
3737
var containsKey = _cache.ContainsKey(compositeKey);
3838
if (containsKey)
3939
{
40-
return _cache[compositeKey];
40+
var result = _cache[compositeKey];
41+
42+
//IMPORTANT: we must clone to resolve, see: http://issues.umbraco.org/issue/U4-4259
43+
return (IEntity)result.DeepClone();
4144
}
4245

4346
return null;
@@ -56,7 +59,12 @@ select GetCompositeId(type, id)
5659
into key
5760
let containsKey = _cache.ContainsKey(key)
5861
where containsKey
59-
select _cache[key]).ToList();
62+
select _cache[key]
63+
into result
64+
//don't return null objects
65+
where result != null
66+
//IMPORTANT: we must clone to resolve, see: http://issues.umbraco.org/issue/U4-4259
67+
select (IEntity)result.DeepClone()).ToList();
6068
return list;
6169
}
6270

@@ -67,7 +75,14 @@ where containsKey
6775
/// <returns></returns>
6876
public IEnumerable<IEntity> GetAllByType(Type type)
6977
{
70-
var list = _cache.Keys.Where(key => key.Contains(type.Name)).Select(key => _cache[key]).ToList();
78+
var list = _cache.Keys
79+
.Where(key => key.Contains(type.Name))
80+
.Select(key => _cache[key])
81+
//don't return null objects
82+
.Where(result => result != null)
83+
//IMPORTANT: we must clone to resolve, see: http://issues.umbraco.org/issue/U4-4259
84+
.Select(result => (IEntity)result.DeepClone())
85+
.ToList();
7186
return list;
7287
}
7388

@@ -78,6 +93,9 @@ public IEnumerable<IEntity> GetAllByType(Type type)
7893
/// <param name="entity"></param>
7994
public void Save(Type type, IEntity entity)
8095
{
96+
//IMPORTANT: we must clone to store, see: http://issues.umbraco.org/issue/U4-4259
97+
entity = (IEntity)entity.DeepClone();
98+
8199
_cache.AddOrUpdate(GetCompositeId(type, entity.Id), entity, (x, y) => entity);
82100
}
83101

src/Umbraco.Core/Persistence/Caching/RuntimeCacheProvider.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,11 @@ public IEntity GetById(Type type, Guid id)
6767
{
6868
//ensure the key doesn't exist anymore in the tracker
6969
_keyTracker.Remove(key);
70+
return null;
7071
}
71-
return result;
72+
73+
//IMPORTANT: we must clone to resolve, see: http://issues.umbraco.org/issue/U4-4259
74+
return (IEntity)result.DeepClone();
7275
}
7376

7477
public IEnumerable<IEntity> GetByIds(Type type, List<Guid> ids)
@@ -88,7 +91,8 @@ public IEnumerable<IEntity> GetByIds(Type type, List<Guid> ids)
8891
}
8992
else
9093
{
91-
collection.Add(result);
94+
//IMPORTANT: we must clone to resolve, see: http://issues.umbraco.org/issue/U4-4259
95+
collection.Add((IEntity)result.DeepClone());
9296
}
9397
}
9498
return collection;
@@ -113,7 +117,8 @@ public IEnumerable<IEntity> GetAllByType(Type type)
113117
}
114118
else
115119
{
116-
collection.Add(result);
120+
//IMPORTANT: we must clone to resolve, see: http://issues.umbraco.org/issue/U4-4259
121+
collection.Add((IEntity)result.DeepClone());
117122
}
118123
}
119124
}
@@ -122,6 +127,9 @@ public IEnumerable<IEntity> GetAllByType(Type type)
122127

123128
public void Save(Type type, IEntity entity)
124129
{
130+
//IMPORTANT: we must clone to store, see: http://issues.umbraco.org/issue/U4-4259
131+
entity = (IEntity)entity.DeepClone();
132+
125133
var key = GetCompositeId(type, entity.Id);
126134

127135
_keyTracker.TryAdd(key);

0 commit comments

Comments
 (0)