Akavache简明使用指南

// 初始化
Akavache.Registrations.Start(applicationName: "AkavacheExperiment");
// 默认提供了四个常用持久化的方式:
// LocalMachine:默认持久化的文件保存在%LOCALAPPDATA%ApplicationNameBlobCache文件夹下
// UserAccount:默认持久化的文件保存在%APPDATA%ApplicationNameBlobCache文件夹下
// Secure:默认持久化的文件保存在%APPDATA%ApplicationNameSecretCache文件夹下,数据加密
// InMemory:默认保存在内存中,数据加密
BlobCache.Secure
    .GetOrFetchObject("Key", () => GetTable())
    .Catch<DataTable, Exception>(ex =>
    {
        return Observable.Return(new DataTable());
    })
    .Subscribe(ar =>
    {
        dataReady = true;
    });

如果要修改默认JsonSerializerSettings

var jsonSerializerSettings = new JsonSerializerSettings
{
    Converters = new JsonConverter[] { new DataTableConverter() }
};
Splat.Locator.CurrentMutable.RegisterConstant(jsonSerializerSettings, typeof(JsonSerializerSettings));

如果要修改持久化的文件路径,自己实例化一个SQLitePersistentBlobCache或者SQLiteEncryptedBlobCache,然后调用实例即可

var blobCache= new SQLitePersistentBlobCache(Path.Combine(Application.StartupPath, "LocalCache", "LocalCache.bin"));

 在Akavache中还发现了这样一个类,KeyedOperationQueue,翻译过来就是“带有名称的操作队列”,可以把操作分到不同名称的队列当中,队列中的操作按顺序依次执行

转帖一个测试用例体会下

[Fact]
public void KeyedOperationQueueCorrectlyShutsDown()
{
   var fixture = new KeyedOperationQueue();
   var op1 = new Subject<int>();
   var op2 = new Subject<int>();
   var op3 = new Subject<int>();
   bool isCompleted = false;

   int op1Result = 0, op2Result = 0, op3Result = 0;

   fixture.EnqueueObservableOperation("foo", () => op1).Subscribe(x => op1Result = x);
   fixture.EnqueueObservableOperation("bar", () => op2).Subscribe(x => op2Result = x);

   // Shut down the queue, shouldn't be completed until op1 and op2 complete
   fixture.ShutdownQueue().Subscribe(_ => isCompleted = true);
   Assert.False(isCompleted);

   op1.OnNext(1);
   op1.OnCompleted();
   Assert.False(isCompleted);
   Assert.Equal(1, op1Result);

   op2.OnNext(2);
   op2.OnCompleted();
   Assert.True(isCompleted);
   Assert.Equal(2, op2Result);

   // We've already shut down, new ops should be ignored
   fixture.EnqueueObservableOperation("foo", () => op3).Subscribe(x => op3Result = x);
   op3.OnNext(3);
   op3.OnCompleted();
   Assert.Equal(0, op3Result);
}
原文地址:https://www.cnblogs.com/s5689412/p/14768476.html