Ninja Database Pro
CacheExpirationTime Property
Example 



KellermanSoftware.NinjaDatabasePro Namespace > NinjaDbPro Class : CacheExpirationTime Property
By default an interal cache of objects is maintained for performance. This is the time after which an item will be expired in the cache if it is not used. The default is 20 minutes for all platforms
Syntax
Public Property CacheExpirationTime As TimeSpan
Dim instance As NinjaDbPro
Dim value As TimeSpan
 
instance.CacheExpirationTime = value
 
value = instance.CacheExpirationTime
public TimeSpan CacheExpirationTime {get; set;}
public:
property TimeSpan CacheExpirationTime {
   TimeSpan get();
   void set (    TimeSpan value);
}
Example
public void CacheUsageTest()
{
    int max = 1000;
 
    NinjaDbPro db = new NinjaDbPro("MyDatabaseDirectory", "MyDatabaseName");
    db.OpenDatabase();
 
    //Setting the MaxCacheSize to zero disables the cache
    //By default the MaxCacheSize 5MB
    db.MaxCacheSize = 0;
 
    //Save some people
    for (int i = 1; i <= max; i++)
    {
        Person person = new Person();
        person.Name = "John " + i;
 
        db.Save(person);
    }
 
    //Load with the cache disabled
    Stopwatch watch = new Stopwatch();
    watch.Start();
    List<Person> persons= db.LoadAll<Person>();
    watch.Stop();
 
    long noCacheTime = watch.ElapsedMilliseconds;
    Console.WriteLine("Elapsed Load Without Cache {0}ms", noCacheTime);
 
    //Set the cache to 5MB
    //By default the MaxCacheSize 5MB
    db.MaxCacheSize = 5000*1024;
 
    //Set the cache expiration time to 5 minutes
    //The default CacheExpirationTime is 20 minutes for all platforms
    db.CacheExpirationTime = new TimeSpan(0, 0, 5);
    
    //This loads it into the cache
    persons = db.LoadAll<Person>();
 
    //Load from the cache
    watch.Reset();
    watch.Start();
    persons = db.LoadAll<Person>();
    watch.Stop();
 
    long withCacheTime = watch.ElapsedMilliseconds;
    Console.WriteLine("Elapsed Load WITH Cache {0}ms", withCacheTime);
 
    db.CloseDatabase();
}
Public Sub CacheUsageTest()
    Dim max As Integer = 1000
 
    Dim db As New NinjaDbPro("MyDatabaseDirectory", "MyDatabaseName")
    db.OpenDatabase()
 
    'Setting the MaxCacheSize to zero disables the cache
    'By default the MaxCacheSize 5MB
    db.MaxCacheSize = 0
 
    'Save some people
    For i As Integer = 1 To max
        Dim person As New Person()
        person.Name = "John " & i
 
        db.Save(person)
    Next i
 
    'Load with the cache disabled
    Dim watch As New Stopwatch()
    watch.Start()
    Dim persons As List(Of Person)= db.LoadAll(Of Person)()
    watch.Stop()
 
    Dim noCacheTime As Long = watch.ElapsedMilliseconds
    Console.WriteLine("Elapsed Load Without Cache {0}ms", noCacheTime)
 
    'Set the cache to 5MB
    'By default the MaxCacheSize 5MB
    db.MaxCacheSize = 5000*1024
 
    'Set the cache expiration time to 5 minutes
    'The default CacheExpirationTime is 20 minutes for all platforms
    db.CacheExpirationTime = New TimeSpan(0, 0, 5)
 
    'This loads it into the cache
    persons = db.LoadAll(Of Person)()
 
    'Load from the cache
    watch.Reset()
    watch.Start()
    persons = db.LoadAll(Of Person)()
    watch.Stop()
 
    Dim withCacheTime As Long = watch.ElapsedMilliseconds
    Console.WriteLine("Elapsed Load WITH Cache {0}ms", withCacheTime)
 
    db.CloseDatabase()
End Sub
Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

NinjaDbPro Class
NinjaDbPro Members
MaxCacheSize Property