<span class="token keyword keyword-public">public</span> <span class="token keyword keyword-class">class</span> <span class="token class-name">InMemoryCacheService</span> <span class="token punctuation">:</span> <span class="token type-list"><span class="token class-name">IInMemoryCacheService</span></span>
<span class="token punctuation">{</span>
<span class="token doc-comment comment">/// <span class="token tag"><span class="token punctuation"><</span>summary<span class="token punctuation">></span></span></span>
<span class="token doc-comment comment">/// MemoryCache</span>
<span class="token doc-comment comment">/// <span class="token tag"><span class="token punctuation"></</span>summary<span class="token punctuation">></span></span></span>
<span class="token keyword keyword-private">private</span> <span class="token keyword keyword-readonly">readonly</span> <span class="token class-name">IMemoryCache</span> _memoryCache<span class="token punctuation">;</span>
<span class="token doc-comment comment">/// <span class="token tag"><span class="token punctuation"><</span>summary<span class="token punctuation">></span></span></span>
<span class="token doc-comment comment">/// MemoryCacheService</span>
<span class="token doc-comment comment">/// <span class="token tag"><span class="token punctuation"></</span>summary<span class="token punctuation">></span></span></span>
<span class="token doc-comment comment">/// <span class="token tag"><span class="token punctuation"><</span>param <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>memoryCache<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token punctuation"></</span>param<span class="token punctuation">></span></span></span>
<span class="token keyword keyword-public">public</span> <span class="token function">InMemoryCacheService</span><span class="token punctuation">(</span><span class="token class-name">IMemoryCache</span> memoryCache<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
_memoryCache <span class="token operator">=</span> memoryCache<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token doc-comment comment">/// <span class="token tag"><span class="token punctuation"><</span>summary<span class="token punctuation">></span></span></span>
<span class="token doc-comment comment">/// Retrieves the value associated with the given key from the cache.</span>
<span class="token doc-comment comment">/// <span class="token tag"><span class="token punctuation"></</span>summary<span class="token punctuation">></span></span></span>
<span class="token doc-comment comment">/// <span class="token tag"><span class="token punctuation"><</span>param <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>key<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token punctuation"></</span>param<span class="token punctuation">></span></span></span>
<span class="token doc-comment comment">/// <span class="token tag"><span class="token punctuation"><</span>returns<span class="token punctuation">></span></span><span class="token tag"><span class="token punctuation"></</span>returns<span class="token punctuation">></span></span></span>
<span class="token keyword keyword-public">public</span> <span class="token return-type class-name">T</span> <span class="token generic-method"><span class="token function">Get</span><span class="token generic class-name"><span class="token punctuation"><</span>T<span class="token punctuation">></span></span></span><span class="token punctuation">(</span><span class="token class-name"><span class="token keyword keyword-string">string</span></span> key<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword keyword-return">return</span> _memoryCache<span class="token punctuation">.</span><span class="token generic-method"><span class="token function">Get</span><span class="token generic class-name"><span class="token punctuation"><</span>T<span class="token punctuation">></span></span></span><span class="token punctuation">(</span>key<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token doc-comment comment">/// <span class="token tag"><span class="token punctuation"><</span>summary<span class="token punctuation">></span></span></span>
<span class="token doc-comment comment">/// Stores the given value in the cache associated with the specified key.</span>
<span class="token doc-comment comment">/// <span class="token tag"><span class="token punctuation"></</span>summary<span class="token punctuation">></span></span></span>
<span class="token doc-comment comment">/// <span class="token tag"><span class="token punctuation"><</span>param <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>key<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token punctuation"></</span>param<span class="token punctuation">></span></span></span>
<span class="token doc-comment comment">/// <span class="token tag"><span class="token punctuation"><</span>param <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>value<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token punctuation"></</span>param<span class="token punctuation">></span></span></span>
<span class="token doc-comment comment">/// <span class="token tag"><span class="token punctuation"><</span>param <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>expirationTime<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token punctuation"></</span>param<span class="token punctuation">></span></span></span>
<span class="token keyword keyword-public">public</span> <span class="token return-type class-name"><span class="token keyword keyword-void">void</span></span> <span class="token generic-method"><span class="token function">AddOrUpdate</span><span class="token generic class-name"><span class="token punctuation"><</span>T<span class="token punctuation">></span></span></span><span class="token punctuation">(</span><span class="token class-name"><span class="token keyword keyword-string">string</span></span> key<span class="token punctuation">,</span> <span class="token class-name">T</span> <span class="token keyword keyword-value">value</span><span class="token punctuation">,</span> <span class="token class-name">TimeSpan</span> expirationTime<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token class-name"><span class="token keyword keyword-var">var</span></span> cacheEntryOptions <span class="token operator">=</span> <span class="token keyword keyword-new">new</span> <span class="token constructor-invocation class-name">MemoryCacheEntryOptions</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">SetAbsoluteExpiration</span><span class="token punctuation">(</span>expirationTime<span class="token punctuation">)</span><span class="token punctuation">;</span>
_memoryCache<span class="token punctuation">.</span><span class="token function">Set</span><span class="token punctuation">(</span>key<span class="token punctuation">,</span> <span class="token keyword keyword-value">value</span><span class="token punctuation">,</span> cacheEntryOptions<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token doc-comment comment">/// <span class="token tag"><span class="token punctuation"><</span>summary<span class="token punctuation">></span></span></span>
<span class="token doc-comment comment">/// Stores the given value in the cache associated with the specified key.</span>
<span class="token doc-comment comment">/// <span class="token tag"><span class="token punctuation"></</span>summary<span class="token punctuation">></span></span></span>
<span class="token doc-comment comment">/// <span class="token tag"><span class="token punctuation"><</span>typeparam <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>T<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token punctuation"></</span>typeparam<span class="token punctuation">></span></span></span>
<span class="token doc-comment comment">/// <span class="token tag"><span class="token punctuation"><</span>param <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>key<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token punctuation"></</span>param<span class="token punctuation">></span></span></span>
<span class="token doc-comment comment">/// <span class="token tag"><span class="token punctuation"><</span>param <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>value<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token punctuation"></</span>param<span class="token punctuation">></span></span></span>
<span class="token keyword keyword-public">public</span> <span class="token return-type class-name"><span class="token keyword keyword-void">void</span></span> <span class="token generic-method"><span class="token function">AddOrUpdate</span><span class="token generic class-name"><span class="token punctuation"><</span>T<span class="token punctuation">></span></span></span><span class="token punctuation">(</span><span class="token class-name"><span class="token keyword keyword-string">string</span></span> key<span class="token punctuation">,</span> <span class="token class-name">T</span> <span class="token keyword keyword-value">value</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token class-name"><span class="token keyword keyword-var">var</span></span> cacheEntryOptions <span class="token operator">=</span> <span class="token keyword keyword-new">new</span> <span class="token constructor-invocation class-name">MemoryCacheEntryOptions</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">SetPriority</span><span class="token punctuation">(</span>CacheItemPriority<span class="token punctuation">.</span>NeverRemove<span class="token punctuation">)</span><span class="token punctuation">;</span>
_memoryCache<span class="token punctuation">.</span><span class="token function">Set</span><span class="token punctuation">(</span>key<span class="token punctuation">,</span> <span class="token keyword keyword-value">value</span><span class="token punctuation">,</span> cacheEntryOptions<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token doc-comment comment">/// <span class="token tag"><span class="token punctuation"><</span>summary<span class="token punctuation">></span></span></span>
<span class="token doc-comment comment">/// Removes the value associated with the specified key from the cache.</span>
<span class="token doc-comment comment">/// <span class="token tag"><span class="token punctuation"></</span>summary<span class="token punctuation">></span></span></span>
<span class="token doc-comment comment">/// <span class="token tag"><span class="token punctuation"><</span>param <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>key<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token punctuation"></</span>param<span class="token punctuation">></span></span></span>
<span class="token keyword keyword-public">public</span> <span class="token return-type class-name"><span class="token keyword keyword-void">void</span></span> <span class="token function">Delete</span><span class="token punctuation">(</span><span class="token class-name"><span class="token keyword keyword-string">string</span></span> key<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
_memoryCache<span class="token punctuation">.</span><span class="token function">Remove</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token doc-comment comment">/// <span class="token tag"><span class="token punctuation"><</span>summary<span class="token punctuation">></span></span></span>
<span class="token doc-comment comment">/// Checks if the specified key exists in the cache.</span>
<span class="token doc-comment comment">/// <span class="token tag"><span class="token punctuation"></</span>summary<span class="token punctuation">></span></span></span>
<span class="token doc-comment comment">/// <span class="token tag"><span class="token punctuation"><</span>param <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>key<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token punctuation"></</span>param<span class="token punctuation">></span></span></span>
<span class="token doc-comment comment">/// <span class="token tag"><span class="token punctuation"><</span>returns<span class="token punctuation">></span></span><span class="token tag"><span class="token punctuation"></</span>returns<span class="token punctuation">></span></span></span>
<span class="token keyword keyword-public">public</span> <span class="token return-type class-name"><span class="token keyword keyword-bool">bool</span></span> <span class="token generic-method"><span class="token function">IsExists</span><span class="token generic class-name"><span class="token punctuation"><</span>T<span class="token punctuation">></span></span></span><span class="token punctuation">(</span><span class="token class-name"><span class="token keyword keyword-string">string</span></span> key<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword keyword-return">return</span> _memoryCache<span class="token punctuation">.</span><span class="token generic-method"><span class="token function">TryGetValue</span><span class="token generic class-name"><span class="token punctuation"><</span>T<span class="token punctuation">></span></span></span><span class="token punctuation">(</span>key<span class="token punctuation">,</span> <span class="token keyword keyword-out">out</span> _<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>