博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
RavenDb学习(九)高级特性下半部分
阅读量:6573 次
发布时间:2019-06-24

本文共 5616 字,大约阅读时间需要 18 分钟。

1.聚合缓存RavenDb默认是缓存所有的请求url的,最大的缓存请求数默认是2048documentStore.Conventions.ShouldCacheRequest = url => true;documentStore.MaxNumberOfCachedRequests = 2048;如果开启这个选项,RavenDb直接从缓存当中读取数据,而不是从服务端。//关闭跟踪documentStore.Conventions.ShouldAggressiveCacheTrackChanges = false;using (session.Advanced.DocumentStore.AggressivelyCacheFor(TimeSpan.FromMinutes(5))){    var user = session.Load
("users/1");}using (session.Advanced.DocumentStore.AggressivelyCacheFor(TimeSpan.FromMinutes(5))){ var user = session.Load
("users/1");}//启动强制检查更新,在SaveChanges之后documentStore.Conventions.ShouldSaveChangesForceAggresiveCacheCheck = true;2、客户端ListenerDocument Conflict listeners (IDocumentConflictListener),Document Conversion listeners (IDocumentConversionListener)Document Delete listeners (IDocumentDeleteListener)Document Query listeners (IDocumentQueryListener)Document Store listeners (IDocumentStoreListener)例子1:public class TakeNewestConflictResolutionListener : IDocumentConflictListener{ public bool TryResolveConflict(string key, JsonDocument[] conflictedDocs, out JsonDocument resolvedDocument) { var maxDate = conflictedDocs.Max(x => x.LastModified); resolvedDocument = conflictedDocs.FirstOrDefault(x => x.LastModified == maxDate); return resolvedDocument != null; }}例子2:public class Custom{ public string Id { get; set; } public string Name { get; set; } public string Value { get; set; }} public class MetadataToPropertyConversionListener : IDocumentConversionListener{ public void EntityToDocument(string key, object entity, RavenJObject document, RavenJObject metadata) { if (entity is Custom == false) return; document.Remove("Value"); } public void DocumentToEntity(string key, object entity, RavenJObject document, RavenJObject metadata) { if (entity is Custom == false) return; ((Custom)entity).Value = metadata.Value
("Raven-Document-Revision"); }}例子3:public class FailDelete : IDocumentDeleteListener{ public void BeforeDelete(string key, object entityInstance, RavenJObject metadata) { throw new NotSupportedException(); }}例子4:public class NonStaleQueryListener : IDocumentQueryListener{ public void BeforeQueryExecuted(IDocumentQueryCustomization customization) { customization.WaitForNonStaleResults(); }}例子5:public class FilterForbiddenKeysDocumentListener : IDocumentStoreListener{ private readonly IList
forbiddenKeys = new List
{ "system" }; public bool BeforeStore(string key, object entityInstance, RavenJObject metadata, RavenJObject original) { return this.forbiddenKeys.Any(x => x.Equals(key, StringComparison.InvariantCultureIgnoreCase)) == false; } public void AfterStore(string key, object entityInstance, RavenJObject metadata) { }}3、批量插入//批量插入代码示例using (var bulkInsert = store.BulkInsert()){ for (int i = 0; i < 1000 * 1000; i++) { bulkInsert.Store(new User { Name = "Users #" + i }); }}//批量插入接口原型public interface IDocumentStore{ BulkInsertOperation BulkInsert(string database = null, BulkInsertOptions options = null);}//批量插入选项原型public class BulkInsertOptions{ public bool CheckForUpdates { get; set; } public bool CheckReferencesInIndexes { get; set; } public int BatchSize { get; set; }}//批量插入操作原型public class BulkInsertOperation{ public delegate void BeforeEntityInsert(string id, RavenJObject data, RavenJObject metadata); public event BeforeEntityInsert OnBeforeEntityInsert = delegate { }; public event Action
Report { ... } public void Store(object entity) { ... } public void Store(object entity, string id) { ... }}4、流式查询默认的分页查询的,但是有时候,我们需要一次性查询,RavenDb提供了一种流式查询的方式//正常查询var query = session.Query
("Users/ByActive").Where(x => x.Active);using (var enumerator = session.Advanced.Stream(query)){ while (enumerator.MoveNext()) { User activeUser = enumerator.Current.Document; }}//lucene查询var luceneQuery = session.Advanced.LuceneQuery
("Users/ByActive").Where("Active:true");using (var enumerator = session.Advanced.Stream(luceneQuery)){ while (enumerator.MoveNext()) { User activeUser = enumerator.Current.Document; }}//接口原型QueryHeaderInformation queryHeaderInformation;session.Advanced.Stream(query, out queryHeaderInformation);public class QueryHeaderInformation{ public string Index { get; set; } public bool IsStable { get; set; } public DateTime IndexTimestamp { get; set; } public int TotalResults { get; set; } public Etag ResultEtag { get; set; } public Etag IndexEtag { get; set; }}//分页using (var enumerator = session.Advanced.Stream
(fromEtag: Etag.Empty, start: 0, pageSize: int.MaxValue)){ while (enumerator.MoveNext()) { User activeUser = enumerator.Current.Document; }}//using (var enumerator = session.Advanced.Stream
(startsWith: "users/", matches: "*Ra?en", start: 0, pageSize: int.MaxValue)){ while (enumerator.MoveNext()) { User activeUser = enumerator.Current.Document; }}//异步版本using (var asyncSession = store.OpenAsyncSession()){ var query = asyncSession.Query
("Users/ByActive").Where(x => x.Active); using (var enumerator = await asyncSession.Advanced.StreamAsync(query)) { while (await enumerator.MoveNextAsync()) { User activeUser = enumerator.Current.Document; } } using (var enumerator = await asyncSession.Advanced.StreamAsync
(Etag.Empty)) { while (await enumerator.MoveNextAsync()) { User activeUser = enumerator.Current.Document; } }}

 

转载于:https://www.cnblogs.com/cenyuhai/p/3537009.html

你可能感兴趣的文章
Android应用及应用管理
查看>>
2012CSDN年度博客之星评选http://vote.blog.csdn.net/item/blogstar/xyz_lmn
查看>>
Linux系统与网络服务管理技术大全(第2版)
查看>>
window下配置定时任务实现类似linux的cron定时任务
查看>>
铁道部否认被中铁工程等十多家公司老总蹲点讨债
查看>>
js事件---事件流
查看>>
我的友情链接
查看>>
谁拿了最多奖学金
查看>>
详解linux运维工程师入门级必备技能
查看>>
我的友情链接
查看>>
PhoneGap在Microsoft Visual Studio Express For Wi...
查看>>
Shell脚本的模块化和脚本复用
查看>>
暴力删除
查看>>
unable to bind to locking port 7054 within 45000 ms
查看>>
自动化运维之kickstart自动化部署安装操作系统
查看>>
C++前置声明的一个好处与用法
查看>>
Upgrade GI/CRS 11.1.0.7 to 11.2.0.2. Rootupgrade.sh Hanging
查看>>
vue组件样式scoped
查看>>
整站爬虫命令
查看>>
linux下ssh/sftp配置和权限设置
查看>>