adds another test to ensure the cache expiry is working

This commit is contained in:
Shannon
2014-09-26 15:31:37 +10:00
parent 62327a9b58
commit d402f762ae
3 changed files with 32 additions and 3 deletions

View File

@@ -1704,13 +1704,16 @@ namespace Umbraco.Core.Persistence
}
static readonly ObjectCache ObjectCache = new MemoryCache("NPoco");
}
public class PocoData
{
//USE ONLY FOR TESTING
internal static bool UseLongKeys = false;
//USE ONLY FOR TESTING - default is one hr
internal static int SlidingExpirationSeconds = 3600;
public static PocoData ForObject(object o, string primaryKeyName)
{
var t = o.GetType();
@@ -2078,7 +2081,7 @@ namespace Umbraco.Core.Persistence
{
//sliding expiration of 1 hr, if the same key isn't used in this
// timeframe it will be removed from the cache
SlidingExpiration = new TimeSpan(1,0,0)
SlidingExpiration = new TimeSpan(0, 0, SlidingExpirationSeconds)
});
return (value ?? newValue).Value; // Lazy<T> handles the locking itself
@@ -2170,7 +2173,7 @@ namespace Umbraco.Core.Persistence
public TableInfo TableInfo { get; private set; }
public Dictionary<string, PocoColumn> Columns { get; private set; }
static System.Threading.ReaderWriterLockSlim InnerLock = new System.Threading.ReaderWriterLockSlim();
/// <summary>
/// Returns a report of the current cache being utilized by PetaPoco
/// </summary>

View File

@@ -163,6 +163,8 @@ namespace Umbraco.Core.Persistence.Repositories
//TODO: Consider caching implications.
//TODO: We need to add lookups for parentId or path! (i.e. get content in tag group that are descendants of x)
public IEnumerable<TaggedEntity> GetTaggedEntitiesByTagGroup(TaggableObjectTypes objectType, string tagGroup)
{

View File

@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using NUnit.Framework;
using Umbraco.Core;
using Umbraco.Core.Models;
@@ -73,6 +74,29 @@ namespace Umbraco.Tests.Persistence
Assert.AreEqual(1, totalKeys.Count());
}
[Test]
public void Verify_Memory_Expires()
{
Database.PocoData.SlidingExpirationSeconds = 2;
var managedCache = new Database.ManagedCache();
int id1, id2, id3;
string alias;
CreateStuff(out id1, out id2, out id3, out alias);
QueryStuff(id1, id2, id3, alias);
var count1 = managedCache.GetCache().GetCount();
Console.WriteLine("Keys = " + count1);
Assert.Greater(count1, 0);
Thread.Sleep(10000);
var count2 = managedCache.GetCache().GetCount();
Console.WriteLine("Keys = " + count2);
Assert.Less(count2, count1);
}
private void QueryStuff(int id1, int id2, int id3, string alias1)
{
var contentService = ServiceContext.ContentService;