示例#1
0
        public void GetCallsCacheAndSerializer()
        {
            _distributedResponseCache.Get(Key);

            A.CallTo(() => _fakeDistributedCache.Get(Key))
            .MustHaveHappenedOnceExactly();

            A.CallTo(() => _fakeCacheSerializer.Deserialize(_serializedValue))
            .MustHaveHappenedOnceExactly();
        }
        public IResponseCacheEntry Get(string key)
        {
            var serializedValue = _distributedCache.Get(key);

            if (serializedValue == null)
            {
                return(null);
            }

            var deserializedValue = _serializer.Deserialize(serializedValue);

            return(deserializedValue);
        }
        public override async Task InterceptAsync(IRocketMethodInvocation invocation)
        {
            var parameters = invocation.Method.GetParameters();

            //判断Method是否包含ref / out参数
            if (parameters.Any(it => it.IsIn || it.IsOut))
            {
                await invocation.ProceedAsync();
            }
            else
            {
                var distributedCacheAttribute = ReflectionHelper.GetSingleAttributeOrDefault <LocalCacheAttribute> (invocation.Method);
                var cacheKey   = CacheKeyHelper.GetCacheKey(invocation.Method, invocation.Arguments, distributedCacheAttribute.Prefix);
                var returnType = invocation.Method.ReturnType.GetGenericArguments().First();
                try {
                    var cacheValue = await _cache.GetAsync(cacheKey);

                    if (cacheValue != null)
                    {
                        var resultValue = _serializer.Deserialize(cacheValue, returnType);
                        invocation.ReturnValue = invocation.IsAsync() ? _taskResultMethod.MakeGenericMethod(returnType).Invoke(null, new object[] { resultValue }) : resultValue;
                    }
                    else
                    {
                        if (!distributedCacheAttribute.ThreadLock)
                        {
                            await GetResultAndSetCache(invocation, cacheKey, distributedCacheAttribute.Expiration);
                        }
                        else
                        {
                            using (await _lock.LockAsync()) {
                                cacheValue = await _cache.GetAsync(cacheKey);

                                if (cacheValue != null)
                                {
                                    var resultValue = _serializer.Deserialize(cacheValue, returnType);
                                    invocation.ReturnValue = invocation.IsAsync() ? _taskResultMethod.MakeGenericMethod(returnType).Invoke(null, new object[] { resultValue }) : resultValue;
                                }
                                else
                                {
                                    await GetResultAndSetCache(invocation, cacheKey, distributedCacheAttribute.Expiration);
                                }
                            }
                        }
                    }
                } catch (Exception) {
                    await invocation.ProceedAsync();
                }
            }
        }
示例#4
0
        public OrchestrationTests()
        {
            // Setup Distributed Cache
            _fakeDistributedCache = A.Fake <IDistributedCache>();

            A.CallTo(() => _fakeDistributedCache.Get(Key)).Returns(_serializedValue);
            A.CallTo(() => _fakeDistributedCache.GetAsync(Key, default(CancellationToken))).Returns(_serializedValue);

            // Setup Serializer
            _fakeCacheSerializer = A.Fake <IDistributedCacheSerializer>();
            A.CallTo(() => _fakeCacheSerializer.Serialize(_deserializedValue)).Returns(_serializedValue);
            A.CallTo(() => _fakeCacheSerializer.Deserialize(_serializedValue)).Returns(_deserializedValue);

            // Setup DistributedResponseCache
            _distributedResponseCache = new DistributedResponseCache(_fakeDistributedCache, _fakeCacheSerializer);
        }