@@ -130,31 +130,21 @@ internal static partial class CacheConcurrencyStrategyExtensions
130130 /// </summary>
131131 /// <param name="cache">The cache concurrency strategy.</param>
132132 /// <param name="keys">The keys (id) of the objects to get out of the Cache.</param>
133- /// <param name="txTimestamp ">A timestamp prior to the transaction start time</param>
133+ /// <param name="timestamp ">A timestamp prior to the transaction start time</param>
134134 /// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
135135 /// <returns>An array of cached objects or <see langword="null" /></returns>
136136 /// <exception cref="CacheException"></exception>
137- //6.0 TODO: Merge into ICacheConcurrencyStrategy.
138- public static async Task < object [ ] > GetMultipleAsync ( this ICacheConcurrencyStrategy cache , CacheKey [ ] keys , long txTimestamp , CancellationToken cancellationToken )
137+ public static Task < object [ ] > GetManyAsync ( this ICacheConcurrencyStrategy cache , CacheKey [ ] keys , long timestamp , CancellationToken cancellationToken )
139138 {
140- cancellationToken . ThrowIfCancellationRequested ( ) ;
141- switch ( cache )
139+ if ( ! ( cache is IBatchableCacheConcurrencyStrategy batchableCache ) )
142140 {
143- case ReadOnlyCache readOnly :
144- return await ( readOnly . GetMultipleAsync ( keys , txTimestamp , cancellationToken ) ) . ConfigureAwait ( false ) ;
145- case ReadWriteCache readWrite :
146- return await ( readWrite . GetMultipleAsync ( keys , txTimestamp , cancellationToken ) ) . ConfigureAwait ( false ) ;
147- case NonstrictReadWriteCache nonstrictReadWrite :
148- return await ( nonstrictReadWrite . GetMultipleAsync ( keys , txTimestamp , cancellationToken ) ) . ConfigureAwait ( false ) ;
141+ throw new InvalidOperationException ( $ "Cache concurrency strategy { cache . GetType ( ) } does not support batching") ;
149142 }
150-
151- // Fallback to Get
152- var objects = new object [ keys . Length ] ;
153- for ( var i = 0 ; i < keys . Length ; i ++ )
143+ if ( cancellationToken . IsCancellationRequested )
154144 {
155- objects [ i ] = await ( cache . GetAsync ( keys [ i ] , txTimestamp , cancellationToken ) ) . ConfigureAwait ( false ) ;
145+ return Task . FromCanceled < object [ ] > ( cancellationToken ) ;
156146 }
157- return objects ;
147+ return batchableCache . GetManyAsync ( keys , timestamp , cancellationToken ) ;
158148 }
159149
160150 /// <summary>
@@ -170,28 +160,18 @@ public static async Task<object[]> GetMultipleAsync(this ICacheConcurrencyStrate
170160 /// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
171161 /// <returns><see langword="true" /> if the objects were successfully cached.</returns>
172162 /// <exception cref="CacheException"></exception>
173- //6.0 TODO: Merge into ICacheConcurrencyStrategy.
174- public static async Task < bool [ ] > PutMultipleAsync ( this ICacheConcurrencyStrategy cache , CacheKey [ ] keys , object [ ] values , long timestamp ,
163+ public static Task < bool [ ] > PutManyAsync ( this ICacheConcurrencyStrategy cache , CacheKey [ ] keys , object [ ] values , long timestamp ,
175164 object [ ] versions , IComparer [ ] versionComparers , bool [ ] minimalPuts , CancellationToken cancellationToken )
176165 {
177- cancellationToken . ThrowIfCancellationRequested ( ) ;
178- switch ( cache )
166+ if ( ! ( cache is IBatchableCacheConcurrencyStrategy batchableCache ) )
179167 {
180- case ReadOnlyCache readOnly :
181- return await ( readOnly . PutMultipleAsync ( keys , values , timestamp , versions , versionComparers , minimalPuts , cancellationToken ) ) . ConfigureAwait ( false ) ;
182- case ReadWriteCache readWrite :
183- return await ( readWrite . PutMultipleAsync ( keys , values , timestamp , versions , versionComparers , minimalPuts , cancellationToken ) ) . ConfigureAwait ( false ) ;
184- case NonstrictReadWriteCache nonstrictReadWrite :
185- return await ( nonstrictReadWrite . PutMultipleAsync ( keys , values , timestamp , versions , versionComparers , minimalPuts , cancellationToken ) ) . ConfigureAwait ( false ) ;
168+ throw new InvalidOperationException ( $ "Cache concurrency strategy { cache . GetType ( ) } does not support batching") ;
186169 }
187-
188- // Fallback to Put
189- var result = new bool [ keys . Length ] ;
190- for ( var i = 0 ; i < keys . Length ; i ++ )
170+ if ( cancellationToken . IsCancellationRequested )
191171 {
192- result [ i ] = await ( cache . PutAsync ( keys [ i ] , values [ i ] , timestamp , versions [ i ] , versionComparers [ i ] , minimalPuts [ i ] , cancellationToken ) ) . ConfigureAwait ( false ) ;
172+ return Task . FromCanceled < bool [ ] > ( cancellationToken ) ;
193173 }
194- return result ;
174+ return batchableCache . PutManyAsync ( keys , values , timestamp , versions , versionComparers , minimalPuts , cancellationToken ) ;
195175 }
196176 }
197177}
0 commit comments