|
| 1 | +// |
| 2 | +// ICache.cs |
| 3 | +// |
| 4 | +// Author: |
| 5 | +// Craig Fowler <craig@csf-dev.com> |
| 6 | +// |
| 7 | +// Copyright (c) 2015 CSF Software Limited |
| 8 | +// |
| 9 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | +// of this software and associated documentation files (the "Software"), to deal |
| 11 | +// in the Software without restriction, including without limitation the rights |
| 12 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | +// copies of the Software, and to permit persons to whom the Software is |
| 14 | +// furnished to do so, subject to the following conditions: |
| 15 | +// |
| 16 | +// The above copyright notice and this permission notice shall be included in |
| 17 | +// all copies or substantial portions of the Software. |
| 18 | +// |
| 19 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | +// THE SOFTWARE. |
| 26 | + |
| 27 | +using System; |
| 28 | + |
| 29 | +namespace CSF.Caches |
| 30 | +{ |
| 31 | + /// <summary> |
| 32 | + /// Interface for a cache implementation, which caches instances of <typeparamref name="TValue" /> indexed by a |
| 33 | + /// <typeparamref name="TKey" />. |
| 34 | + /// </summary> |
| 35 | + public interface ICache<TKey,TValue> |
| 36 | + { |
| 37 | + #region methods |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Adds an item to the cache. |
| 41 | + /// </summary> |
| 42 | + /// <remarks> |
| 43 | + /// <para> |
| 44 | + /// If an item already exists within the cache, identified by the given key, then the value is not added to the |
| 45 | + /// cache, and <c>false</c> is returned. |
| 46 | + /// </para> |
| 47 | + /// </remarks> |
| 48 | + /// <returns><c>true</c> if a new item was added to the cache; <c>false</c> if not.</returns> |
| 49 | + /// <param name="key">The key at which to store the value.</param> |
| 50 | + /// <param name="value">The value to store.</param> |
| 51 | + bool Add(TKey key, TValue value); |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Removes an item from the cache. |
| 55 | + /// </summary> |
| 56 | + /// <remarks> |
| 57 | + /// <para> |
| 58 | + /// If an item does not exist within the cache, identified by the given key, then nothing is removed and |
| 59 | + /// <c>false</c> is returned. |
| 60 | + /// </para> |
| 61 | + /// </remarks> |
| 62 | + /// <returns><c>true</c> if a an item was removed; <c>false</c> if not.</returns> |
| 63 | + /// <param name="key">The key at which to remove an item.</param> |
| 64 | + bool Remove(TKey key); |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Gets a value indicating whether the cache contains an item identified by the given key. |
| 68 | + /// </summary> |
| 69 | + /// <param name="key">The key at which to search for an item.</param> |
| 70 | + bool Contains(TKey key); |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Attempts to get a value from the cache. |
| 74 | + /// </summary> |
| 75 | + /// <returns><c>true</c>, if a value was found in the cache, <c>false</c> otherwise.</returns> |
| 76 | + /// <param name="key">The key for which to retrieve a value.</param> |
| 77 | + /// <param name="value">Exposes the value retrieved from the cache. The value of this parameter is undefined if no value was found.</param> |
| 78 | + bool TryGet(TKey key, out TValue value); |
| 79 | + |
| 80 | + /// <summary> |
| 81 | + /// Gets a value from the cache, raising an exception if no value is available. |
| 82 | + /// </summary> |
| 83 | + /// <exception cref="NotAvailableInCacheException">If the cache does not contain a value for the given key.</exception> |
| 84 | + /// <param name="key">The key at which to get a value.</param> |
| 85 | + TValue Get(TKey key); |
| 86 | + |
| 87 | + /// <summary> |
| 88 | + /// Attempts to get a value from the cache, executing a function to create a value (and add that to the cache) if |
| 89 | + /// one is not already contained. |
| 90 | + /// </summary> |
| 91 | + /// <returns>The cached value, or the newly-created/added one.</returns> |
| 92 | + /// <param name="key">The key for the value.</param> |
| 93 | + /// <param name="valueFactory">A delegate with which to create an instance of the value if it is required.</param> |
| 94 | + TValue GetOrAdd(TKey key, Func<TValue> valueFactory); |
| 95 | + |
| 96 | + /// <summary> |
| 97 | + /// Attempts to get a value from the cache, executing a function to create a value (and add that to the cache) if |
| 98 | + /// one is not already contained. |
| 99 | + /// </summary> |
| 100 | + /// <remarks> |
| 101 | + /// <para> |
| 102 | + /// The <paramref name="cacheHit"/> parameter will be set to <c>true</c> if the request was fulfilled from the |
| 103 | + /// cache, or <c>false</c> if the <paramref name="valueFactory"/> was invoked in order to create a value for the |
| 104 | + /// cache. |
| 105 | + /// </para> |
| 106 | + /// </remarks> |
| 107 | + /// <returns>The cached value, or the newly-created/added one.</returns> |
| 108 | + /// <param name="key">The key for the value.</param> |
| 109 | + /// <param name="valueFactory">A delegate with which to create an instance of the value if it is required.</param> |
| 110 | + /// <param name="cacheHit">Exposes a value indicating whether or not the result was a cache 'hit'.</param> |
| 111 | + TValue GetOrAdd(TKey key, Func<TValue> valueFactory, out bool cacheHit); |
| 112 | + |
| 113 | + #endregion |
| 114 | + } |
| 115 | +} |
| 116 | + |
0 commit comments