1111using System ;
1212using System . Collections . Generic ;
1313using System . Linq ;
14- using System . Runtime . CompilerServices ;
15-
1614using NHibernate . Cfg ;
1715using NHibernate . Util ;
1816
@@ -51,20 +49,26 @@ public Task PreInvalidateAsync(object[] spaces, CancellationToken cancellationTo
5149 }
5250 }
5351
54- public virtual async Task PreInvalidateAsync ( IReadOnlyCollection < string > spaces , CancellationToken cancellationToken )
52+ public virtual Task PreInvalidateAsync ( IReadOnlyCollection < string > spaces , CancellationToken cancellationToken )
5553 {
56- cancellationToken . ThrowIfCancellationRequested ( ) ;
57- if ( spaces . Count == 0 )
58- return ;
59- cancellationToken . ThrowIfCancellationRequested ( ) ;
60-
61- using ( await ( _asyncReaderWriterLock . WriteLockAsync ( ) ) . ConfigureAwait ( false ) )
54+ if ( cancellationToken . IsCancellationRequested )
55+ {
56+ return Task . FromCanceled < object > ( cancellationToken ) ;
57+ }
58+ try
6259 {
60+ if ( spaces . Count == 0 )
61+ return Task . CompletedTask ;
62+
6363 //TODO: to handle concurrent writes correctly, this should return a Lock to the client
6464 var ts = _updateTimestamps . NextTimestamp ( ) + _updateTimestamps . Timeout ;
65- await ( SetSpacesTimestampAsync ( spaces , ts , cancellationToken ) ) . ConfigureAwait ( false ) ;
65+ return SetSpacesTimestampAsync ( spaces , ts , cancellationToken ) ;
6666 //TODO: return new Lock(ts);
6767 }
68+ catch ( Exception ex )
69+ {
70+ return Task . FromException < object > ( ex ) ;
71+ }
6872 }
6973
7074 //Since v5.1
@@ -86,21 +90,27 @@ public Task InvalidateAsync(object[] spaces, CancellationToken cancellationToken
8690 }
8791 }
8892
89- public virtual async Task InvalidateAsync ( IReadOnlyCollection < string > spaces , CancellationToken cancellationToken )
93+ public virtual Task InvalidateAsync ( IReadOnlyCollection < string > spaces , CancellationToken cancellationToken )
9094 {
91- cancellationToken . ThrowIfCancellationRequested ( ) ;
92- if ( spaces . Count == 0 )
93- return ;
94- cancellationToken . ThrowIfCancellationRequested ( ) ;
95-
96- using ( await ( _asyncReaderWriterLock . WriteLockAsync ( ) ) . ConfigureAwait ( false ) )
95+ if ( cancellationToken . IsCancellationRequested )
96+ {
97+ return Task . FromCanceled < object > ( cancellationToken ) ;
98+ }
99+ try
97100 {
101+ if ( spaces . Count == 0 )
102+ return Task . CompletedTask ;
103+
98104 //TODO: to handle concurrent writes correctly, the client should pass in a Lock
99105 long ts = _updateTimestamps . NextTimestamp ( ) ;
100106 //TODO: if lock.getTimestamp().equals(ts)
101107 if ( log . IsDebugEnabled ( ) )
102108 log . Debug ( "Invalidating spaces [{0}]" , StringHelper . CollectionToString ( spaces ) ) ;
103- await ( SetSpacesTimestampAsync ( spaces , ts , cancellationToken ) ) . ConfigureAwait ( false ) ;
109+ return SetSpacesTimestampAsync ( spaces , ts , cancellationToken ) ;
110+ }
111+ catch ( Exception ex )
112+ {
113+ return Task . FromException < object > ( ex ) ;
104114 }
105115 }
106116
@@ -127,13 +137,9 @@ public virtual async Task<bool> IsUpToDateAsync(ISet<string> spaces, long timest
127137 cancellationToken . ThrowIfCancellationRequested ( ) ;
128138 if ( spaces . Count == 0 )
129139 return true ;
130- cancellationToken . ThrowIfCancellationRequested ( ) ;
131140
132- using ( await ( _asyncReaderWriterLock . ReadLockAsync ( ) ) . ConfigureAwait ( false ) )
133- {
134- var lastUpdates = await ( _updateTimestamps . GetManyAsync ( spaces . ToArray < object > ( ) , cancellationToken ) ) . ConfigureAwait ( false ) ;
135- return lastUpdates . All ( lastUpdate => ! IsOutdated ( lastUpdate as long ? , timestamp ) ) ;
136- }
141+ var lastUpdates = await ( _updateTimestamps . GetManyAsync ( spaces . ToArray < object > ( ) , cancellationToken ) ) . ConfigureAwait ( false ) ;
142+ return lastUpdates . All ( lastUpdate => ! IsOutdated ( lastUpdate as long ? , timestamp ) ) ;
137143 }
138144
139145 public virtual async Task < bool [ ] > AreUpToDateAsync ( ISet < string > [ ] spaces , long [ ] timestamps , CancellationToken cancellationToken )
@@ -152,25 +158,21 @@ public virtual async Task<bool[]> AreUpToDateAsync(ISet<string>[] spaces, long[]
152158 return ArrayHelper . Fill ( true , spaces . Length ) ;
153159
154160 var keys = allSpaces . ToArray < object > ( ) ;
155- cancellationToken . ThrowIfCancellationRequested ( ) ;
156161
157- using ( await ( _asyncReaderWriterLock . ReadLockAsync ( ) ) . ConfigureAwait ( false ) )
158- {
159- var index = 0 ;
160- var lastUpdatesBySpace =
161- ( await ( _updateTimestamps
162- . GetManyAsync ( keys , cancellationToken ) ) . ConfigureAwait ( false ) )
163- . ToDictionary ( u => keys [ index ++ ] , u => u as long ? ) ;
164-
165- var results = new bool [ spaces . Length ] ;
166- for ( var i = 0 ; i < spaces . Length ; i ++ )
167- {
168- var timestamp = timestamps [ i ] ;
169- results [ i ] = spaces [ i ] . All ( space => ! IsOutdated ( lastUpdatesBySpace [ space ] , timestamp ) ) ;
170- }
162+ var index = 0 ;
163+ var lastUpdatesBySpace =
164+ ( await ( _updateTimestamps
165+ . GetManyAsync ( keys , cancellationToken ) ) . ConfigureAwait ( false ) )
166+ . ToDictionary ( u => keys [ index ++ ] , u => u as long ? ) ;
171167
172- return results ;
168+ var results = new bool [ spaces . Length ] ;
169+ for ( var i = 0 ; i < spaces . Length ; i ++ )
170+ {
171+ var timestamp = timestamps [ i ] ;
172+ results [ i ] = spaces [ i ] . All ( space => ! IsOutdated ( lastUpdatesBySpace [ space ] , timestamp ) ) ;
173173 }
174+
175+ return results ;
174176 }
175177 }
176178}
0 commit comments