The CachingStorage is db4o's default storage. The default implementation uses the LRU/Q2 caching mechanism to reduce disk access times and to improve performance. The cache is characterized by the amount of pages that can be utilized and the page size. The multiplication of these two parameters gives the maximum cache size that can be used.
IEmbeddedConfiguration configuration = Db4oEmbedded.NewConfiguration();
IStorage fileStorage = new FileStorage();
IStorage cachingStorage = new CachingStorage(fileStorage, 128, 1024);
configuration.File.Storage
= cachingStorage;
IObjectContainer container = Db4oEmbedded.OpenFile(configuration, "database.db4o");
Dim configuration As IEmbeddedConfiguration = Db4oEmbedded.NewConfiguration() Dim fileStorage As IStorage = New FileStorage() Dim cachingStorage As IStorage = New CachingStorage(fileStorage, 128, 1024) configuration.File.Storage = cachingStorage Dim container As IObjectContainer = Db4oEmbedded.OpenFile(configuration, "database.db4o")
The CachingStorage takes two parameters, the page count and the page size. Bigger page size means faster handling of information as there is no need to switch between pages for longer. On the other hand a bigger page size will mean higher memory consumption, as memory will be reserved for the whole page size, when the page is needed. Modify these values and run performance tests to find the best performance/memory consumption combination for your system. The default values are the following:
Page count = 64 Page size = 1024KB
This gives a total of 64 KB of cache memory.
By default db4o uses a LRU/Q2 caching strategy. You can more about the LRU/Q2 cache on the Internet or you can look for the concrete implementation in db4o source code: LRU2QCache, LRU2QXCache and LRUCache. The LRU2QCache is a simplified implementation of the 2 Queue algorithm described here:http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1..34.2641
It's possible to exchange the cache-type. Inherit from the CachingStorage-class and overwrite the new cache method. There you return you're caching implementation. For example use a simple LRU-Cache.
public class LRUCachingStorage : CachingStorage { private readonly int pageCount; public LRUCachingStorage(IStorage storage):base(storage) { this.pageCount = 128; } public LRUCachingStorage(IStorage storage, int pageCount, int pageSize) : base(storage, pageCount, pageSize) { this.pageCount = pageCount; } protected override ICache4 NewCache() { return CacheFactory.NewLRUCache(pageCount); } }
Public Class LRUCachingStorage Inherits CachingStorage Private ReadOnly pageCount As Integer Public Sub New(ByVal storage As IStorage) MyBase.New(storage) Me.pageCount = 128 End Sub Public Sub New(ByVal storage As IStorage, ByVal pageCount As Integer, ByVal pageSize As Integer) MyBase.New(storage, pageCount, pageSize) Me.pageCount = pageCount End Sub Protected Overrides Function NewCache() As ICache4 Return CacheFactory.NewLRUCache(pageCount) End Function End Class