我需要一些建议,告诉我应该使用什么样的模式将数据推送/拉入我的应用程序。
我正在编写一个规则引擎,需要在内存中保存大量数据才能足够高效。我有一些相当矛盾的要求;
基本上,我认为我需要将数据推送到应用程序中。
我当前“模式”的简化版本看起来像这样(在psuedo-C#中用记事本写的):
// This interface is implemented by all classes that needs the data
interface IDataSubscriber
{
void RegisterData(Entity data);
}
// This interface is implemented by the data access class
interface IDataProvider
{
void EnsureLoaded(Key dataKey);
void RegisterSubscriber(IDataSubscriber subscriber);
}
class MyClassThatNeedsData : IDataSubscriber
{
IDataProvider _provider;
MyClassThatNeedsData(IDataProvider provider)
{
_provider = provider;
_provider.RegisterSubscriber(this);
}
public void RegisterData(Entity data)
{
// Save data for later
StoreDataInCache(data);
}
void UseData(Key key)
{
// Make sure that the data has been stored in cache
_provider.EnsureLoaded(key);
Entity data = GetDataFromCache(key);
}
}
class MyDataProvider : IDataProvider
{
List<IDataSubscriber> _subscribers;
// Make sure that the data for key has been loaded to all subscribers
public void EnsureLoaded(Key key)
{
if (HasKeyBeenMarkedAsLoaded(key))
return;
PublishDataToSubscribers(key);
MarkKeyAsLoaded(key);
}
// Force all subscribers to get a new version of the data for key
public void ForceReload(Key key)
{
PublishDataToSubscribers(key);
MarkKeyAsLoaded(key);
}
void PublishDataToSubscribers(Key key)
{
Entity data = FetchDataFromStore(key);
foreach(var subscriber in _subscribers)
{
subscriber.RegisterData(data);
}
}
}
// This class will be spun off on startup and should make sure that all data is
// preloaded as quickly as possible
class MyPreloadingThread
{
IDataProvider _provider;
MyPreloadingThread(IDataProvider provider)
{
_provider = provider;
}
void RunInBackground()
{
IEnumerable<Key> allKeys = GetAllKeys();
foreach(var key in allKeys)
{
_provider.EnsureLoaded(key);
}
}
}
我有一种感觉,虽然这不一定是做到这一点的最佳方式。只是解释它的事实似乎需要两页感觉就像一个迹象......
有什么想法吗?那里有任何模式我应该看看?
答案 0 :(得分:1)
毫不含糊地说,它应该是
我的投票 - active object共享队列(总线)+懒惰模式+缓存
答案 1 :(得分:0)
您可以从简单的事情开始,例如基于Gateway pattern的解决方案。然后,您可以尝试通过添加Cash来提高性能。