Exemple #1
0
        internal static IAppCache SetUpCacheEntryGet <T>(this IAppCache mockedCachingService, string cacheEntryKey, T cacheEntryValue)
        {
            EnsureArgument.IsNotNull(mockedCachingService, nameof(mockedCachingService));
            EnsureArgument.IsNotNullOrEmpty(cacheEntryKey, nameof(cacheEntryKey));

            Logger.LogDebug("Setting up cache entry Get/GetOrAdd for '{cacheEntryKey}' (type: '{type}'; value: '{cacheEntryValue}')", cacheEntryKey, typeof(T), cacheEntryValue);

            mockedCachingService.Get <T>(Arg.Is <string>(s => s.Equals(cacheEntryKey))).Returns(cacheEntryValue).AndDoes(x => Logger.LogDebug("Cache Get invoked"));

            mockedCachingService.GetOrAdd(Arg.Is <string>(s => s.Equals(cacheEntryKey)), Arg.Any <Func <ICacheEntry, T> >())
            .Returns(cacheEntryValue)
            .AndDoes(x => Logger.LogDebug("Cache GetOrAdd invoked"));

            //Backwards compatibility
            if (ProjectReflectionShortcuts.GetOrAddWithMemoryCacheEntryOptionsMethod != null)
            {
                Logger.LogDebug("Setting up GetOrAddWithMemoryCacheEntryOptionsMethod");

                ProjectReflectionShortcuts.GetOrAddWithMemoryCacheEntryOptionsMethod.MakeGenericMethod(typeof(T))
                .Invoke(mockedCachingService.Configure(),
                        new object[] { Arg.Is <string>(s => s.Equals(cacheEntryKey)), Arg.Any <Func <ICacheEntry, T> >(), Arg.Any <MemoryCacheEntryOptions>() })
                .Returns(cacheEntryValue)
                .AndDoes(x => Logger.LogDebug("Cache GetOrAdd invoked"));
            }

            mockedCachingService.GetAsync <T>(Arg.Is <string>(s => s.Equals(cacheEntryKey)))
            .Returns(Task.FromResult(cacheEntryValue))
            .AndDoes(x => Logger.LogDebug("Cache GetAsync invoked"));

            mockedCachingService.GetOrAddAsync(Arg.Is <string>(s => s.Equals(cacheEntryKey)), Arg.Any <Func <ICacheEntry, Task <T> > >())
            .Returns(Task.FromResult(cacheEntryValue))
            .AndDoes(x => Logger.LogDebug("Cache GetOrAddAsync invoked"));

            //Backwards compatibility
            if (ProjectReflectionShortcuts.GetOrAddAsyncWithMemoryCacheEntryOptionsMethod != null)
            {
                Logger.LogDebug("Setting up GetOrAddAsyncWithMemoryCacheEntryOptionsMethod");

                ProjectReflectionShortcuts.GetOrAddAsyncWithMemoryCacheEntryOptionsMethod.MakeGenericMethod(typeof(T))
                .Invoke(mockedCachingService.Configure(),
                        new object[] { Arg.Is <string>(s => s.Equals(cacheEntryKey)), Arg.Any <Func <ICacheEntry, Task <T> > >(), Arg.Any <MemoryCacheEntryOptions>() })
                .Returns(Task.FromResult(cacheEntryValue))
                .AndDoes(x => Logger.LogDebug("Cache GetOrAddAsync invoked"));
            }

            return(mockedCachingService);
        }