コード例 #1
0
 private static T GetUsingRwLock <T>(IServiceCache cache, ServiceCacheRegistration registration, object tag, object[] values, string name, string @namespace, bool useDBNull)
 {
     _rwLock.EnterUpgradeableReadLock();
     try
     {
         CacheItemHeader header;
         var             valueAsCache = cache.Get(tag, name, registration, out header);
         if (valueAsCache != null && (!registration.UseHeaders || header != null))
         {
             return(!useDBNull || !(valueAsCache is DBNull) ? (T)valueAsCache : default(T));
         }
         _rwLock.EnterWriteLock();
         try
         {
             valueAsCache = cache.Get(tag, name, registration, out header);
             if (valueAsCache != null && (!registration.UseHeaders || header != null))
             {
                 return(!useDBNull || !(valueAsCache is DBNull) ? (T)valueAsCache : default(T));
             }
             // create
             var value = CreateData <T>(@namespace, registration, tag, values, out header);
             valueAsCache = (!useDBNull || value != null ? (object)value : DBNull.Value);
             cache.Add(tag, name, registration.ItemPolicy, valueAsCache, new ServiceCacheByDispatcher(registration, values, header));
             return(value);
         }
         finally { _rwLock.ExitWriteLock(); }
     }
     finally { _rwLock.ExitUpgradeableReadLock(); }
 }
コード例 #2
0
 public PrimaryItemsController(IPrimaryItemService entityService, IMapper mapper, ILogger <PrimaryItemsController> logger, IServiceCache cache)
 {
     _service = entityService;
     _mapper  = mapper;
     _logger  = logger;
     _cache   = cache;
 }
コード例 #3
0
 private static void SetUsingLock(IServiceCache cache, ServiceCacheRegistration registration, object tag, CacheItemHeader header, bool useDBNull, object value)
 {
     lock (_rwLock)
     {
         var valueAsCache = (!useDBNull || value != null ? (object)value : DBNull.Value);
         cache.Add(tag, header.Item, registration.ItemPolicy, valueAsCache, new ServiceCacheByDispatcher(registration, header.Values, header));
     }
 }
コード例 #4
0
 public AccountController(IUserService userService, IServiceCache serviceCache,
                          IAuthorizationService authorizationService, IEMailService eMailService,
                          IGroupService groupService)
 {
     _userService          = userService;
     _serviceCache         = serviceCache;
     _authorizationService = authorizationService;
     _eMailService         = eMailService;
     _groupService         = groupService;
 }
コード例 #5
0
 public ProdutoService(IServiceCache serviceCache,
                       IProdutoRepository repository,
                       IGeradorGuidService geradorGuidService,
                       ProdutoValidator produtoValidator)
 {
     _serviceCache       = serviceCache;
     _repository         = repository;
     _validator          = produtoValidator;
     _geradorGuidService = geradorGuidService;
 }
コード例 #6
0
        public object GetRequiredService(Type type)
        {
            IServiceCache service = null;

            if (!_cache.TryGetValue(type, out service))
            {
                throw new Exception("获取参数对象没有注入");
            }
            return(service.GetCache(_cache));
        }
コード例 #7
0
 public UsuarioService(IServiceCache serviceCache,
                       IUsuarioRepository repository,
                       IGeradorGuidService geradorGuidService,
                       UsuarioValidator validator)
 {
     _serviceCache       = serviceCache;
     _repository         = repository;
     _geradorGuidService = geradorGuidService;
     _validator          = validator;
 }
コード例 #8
0
 private static void SetUsingRwLock(IServiceCache cache, ServiceCacheRegistration registration, object tag, CacheItemPolicy itemPolicy, CacheItemHeader header, bool useDBNull, object value)
 {
     _rwLock.EnterWriteLock();
     try
     {
         var valueAsCache = (!useDBNull || value != null ? (object)value : DBNull.Value);
         cache.Add(tag, header.Item, itemPolicy, valueAsCache, new ServiceCacheByDispatcher(registration, header.Values, header));
     }
     finally { _rwLock.ExitWriteLock(); }
 }
コード例 #9
0
 public BaseUserService(
     IServiceCache cache,
     IDbContextFactory contextFactory,
     IMapper mapper,
     IHttpContextAccessor httpContextAccessor)
 {
     Cache               = cache;
     _eJContext          = contextFactory.CreateReadonlyDbContext <EJContext>();
     Mapper              = mapper;
     HttpContextAccessor = httpContextAccessor;
 }
コード例 #10
0
        public T GetRequiredService <T>()
        {
            Type          t       = typeof(T);
            IServiceCache service = null;

            if (!_cache.TryGetValue(t, out service))
            {
                throw new Exception("获取参数对象没有注入");
            }
            return((T)service.GetCache(_cache));
        }
コード例 #11
0
 public ComandaService(IServiceCache serviceCache,
                       IComandaRepository repository,
                       IGeradorGuidService geradorGuidService,
                       IMensageriaService mensageriaService,
                       ComandaValidator comandaValidator)
 {
     _serviceCache       = serviceCache;
     _repository         = repository;
     _validador          = comandaValidator;
     _mensageriaService  = mensageriaService;
     _geradorGuidService = geradorGuidService;
 }
コード例 #12
0
 public UserService(
     IServiceCache cache,
     IEMailService eMailService,
     IDbContextFactory contextFactory,
     IMapper mapper, IHttpContextAccessor httpContext,
     IAuthorizationService authorizationService, IConfiguration configuration)
     : base(cache, contextFactory, mapper, httpContext)
 {
     EMailService         = eMailService;
     AuthorizationService = authorizationService;
     SaltConfiguration    = configuration.GetSection("Salt").Get <SaltConfiguration>();
     _eJContext           = contextFactory.CreateReadonlyDbContext <EJContext>();
 }
コード例 #13
0
        /// <summary>
        /// Sends the specified cache.
        /// </summary>
        /// <param name="cache">The cache.</param>
        /// <param name="registration">The registration.</param>
        /// <param name="tag">The tag.</param>
        /// <param name="messages">The messages.</param>
        /// <exception cref="System.NotImplementedException"></exception>
        public void Send(IServiceCache cache, IServiceCacheRegistration registration, object tag, params object[] messages)
        {
            if (cache == null)
            {
                throw new ArgumentNullException("cache");
            }
            if (registration == null)
            {
                throw new ArgumentNullException("registration");
            }
            var registration2 = (registration as ServiceCacheRegistration);

            if (registration2 == null)
            {
                throw new ArgumentException("must be ServiceCacheRegistration", "registration");
            }
            var consumerInfos = registration2.GetConsumersFor(messages);

            if (!consumerInfos.GetEnumerator().MoveNext())
            {
                return;
            }
            //
            var itemPolicy = registration2.ItemPolicy;

            if (itemPolicy == null)
            {
                throw new ArgumentNullException("registration.ItemPolicy");
            }
            var    useDBNull = ((cache.Settings.Options & ServiceCacheOptions.UseDBNullWithRegistrations) == ServiceCacheOptions.UseDBNullWithRegistrations);
            var    distributedServiceCache = cache.BehaveAs <IDistributedServiceCache>();
            object value;

            foreach (var header in cache.Get(tag, registration))
            {
                foreach (var consumerInfo in consumerInfos)
                {
                    if ((value = consumerInfo.Invoke(cache, tag, header)) != null)
                    {
                        if (distributedServiceCache == null)
                        {
                            SetUsingLock(cache, registration2, tag, header, useDBNull, value);
                        }
                        else
                        {
                            SetUsingCas(distributedServiceCache, registration2, tag, header, useDBNull, value);
                        }
                    }
                }
            }
        }
コード例 #14
0
 public void WorkingWithRegistrations()
 {
     IServiceCache cache = null;
     // getting registered MyItem
     var myItem = (string)cache.Get(MyCache.MyItem);
     // getting registered MyItemWithDep
     var myItemWithDependencies = cache.Get <string>(MyCache.MyItemWithDep);
     // getting registered MyItemWithCmd
     var myItemWithCacheCommand = cache.Get <string>(MyCache.MyItemWithCmd);
     // getting an IEnumerable<string> from registered MyItems
     var myItems = cache.GetMany <string>(MyCache.MyItems);
     // getting an IEnumerable<string> from registered MyItems2 based on value
     var myItems2 = cache.GetMany <string>(MyCache.MyItems2, new object[] { 10 });
     // getting an IQueryable<string> from registered MyItemsQuery
     var myItems3 = cache.GetQuery <string>(MyCache.MyItemsQuery);
 }
コード例 #15
0
        /// <summary>
        /// Gets the specified cache.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cache">The cache.</param>
        /// <param name="registration">The registration.</param>
        /// <param name="tag">The tag.</param>
        /// <param name="values">The values.</param>
        /// <returns></returns>
        public T Get <T>(IServiceCache cache, IServiceCacheRegistration registration, object tag, object[] values)
        {
            if (cache == null)
            {
                throw new ArgumentNullException("cache");
            }
            if (registration == null)
            {
                throw new ArgumentNullException("registration");
            }
            var registration2 = (registration as ServiceCacheRegistration);

            if (registration2 == null)
            {
                throw new ArgumentException("must be ServiceCacheRegistration", "registration");
            }
            var itemPolicy = registration2.ItemPolicy;

            if (itemPolicy == null)
            {
                throw new ArgumentNullException("registration.ItemPolicy");
            }
            // fetch from cache
            var    name = registration.AbsoluteName;
            string @namespace;

            if (values != null && values.Length > 0)
            {
                cache = cache.BehaveAs(values, out @namespace);
            }
            else
            {
                @namespace = null;
            }
            var useDBNull = ((cache.Settings.Options & ServiceCacheOptions.UseDBNullWithRegistrations) == ServiceCacheOptions.UseDBNullWithRegistrations);
            var distributedServiceCache = cache.BehaveAs <IDistributedServiceCache>();

            if (distributedServiceCache == null)
            {
                return(GetUsingLock <T>(cache, registration2, tag, values, name, @namespace, useDBNull));
            }
            return(GetUsingCas <T>(distributedServiceCache, registration2, tag, values, name, @namespace, useDBNull));
        }
コード例 #16
0
        //public void Update(IServiceCache cache, IServiceCacheRegistration registration, object tag, object value)
        //{
        //    if (cache == null)
        //        throw new ArgumentNullException("cache");
        //    if (registration == null)
        //        throw new ArgumentNullException("registration");
        //    var registration2 = (registration as ServiceCacheRegistration);
        //    if (registration2 == null)
        //        throw new ArgumentException("must be ServiceCacheRegistration", "registration");
        //    var itemPolicy = registration2.ItemPolicy;
        //    if (itemPolicy == null)
        //        throw new ArgumentNullException("registration.ItemPolicy");
        //    var useDBNull = ((cache.Settings.Options & ServiceCacheOptions.UseDBNullWithRegistrations) == ServiceCacheOptions.UseDBNullWithRegistrations);
        //    var distributedServiceCache = cache.BehaveAs<IDistributedServiceCache>();
        //    if (distributedServiceCache == null)
        //        SetUsingLock(cache, registration2, tag, header, useDBNull, value);
        //    else
        //        SetUsingCas(distributedServiceCache, registration2, tag, header, useDBNull, value);
        //}

        /// <summary>
        /// Removes the specified cache.
        /// </summary>
        /// <param name="cache">The cache.</param>
        /// <param name="registration">The registration.</param>
        public void Remove(IServiceCache cache, IServiceCacheRegistration registration)
        {
            if (cache == null)
            {
                throw new ArgumentNullException("cache");
            }
            if (registration == null)
            {
                throw new ArgumentNullException("registration");
            }
            var registration2 = (registration as ServiceCacheRegistration);

            if (registration2 == null)
            {
                throw new ArgumentException("must be ServiceCacheRegistration", "registration");
            }
            foreach (var name in registration2.Keys)
            {
                cache.Remove(null, name, registration);
            }
        }
コード例 #17
0
 public EmployeesController(IEmployeeService employeeService, IServiceCache serviceCache)
 {
     this.employeeService = employeeService;
     this.serviceCache    = serviceCache;
 }
コード例 #18
0
            //private Func<object, IEnumerable<string>, CacheItemDependency> _dependencyFactory;

            /// <summary>
            /// Initializes a new instance of the <see cref="FileTouchableCacheItemBase"/> class.
            /// </summary>
            /// <param name="parent">The parent.</param>
            /// <param name="base">The @base.</param>
            public FileTouchableCacheItemBase(IServiceCache parent, ITouchableCacheItem @base)
            {
                _parent = parent;
                _base   = @base;
            }
コード例 #19
0
 //private Func<object, IEnumerable<string>, CacheItemDependency> _dependencyFactory;
 /// <summary>
 /// Initializes a new instance of the <see cref="FileTouchableCacheItemBase"/> class.
 /// </summary>
 /// <param name="parent">The parent.</param>
 /// <param name="base">The @base.</param>
 public FileTouchableCacheItemBase(IServiceCache parent, ITouchableCacheItem @base)
 {
     _parent = parent;
     _base = @base;
 }
コード例 #20
0
 public void Initialize()
 {
     Cache = CreateServiceCache();
 }
コード例 #21
0
ファイル: CacheTests.cs プロジェクト: dstarosta/GitProjects
 public void Initialize()
 {
     m_cache = Rest.Configuration.ServiceLocator.GetService<IServiceCache>();
 }
コード例 #22
0
 private object InvokeInternal <T>(IServiceCache cache, object tag, CacheItemHeader header)
 {
     return(ActionInvoke <T>(Message, tag, header.Values, () => (T)cache.Get(null, header.Item)));
 }
コード例 #23
0
 /// <summary>
 /// Invokes the specified cache.
 /// </summary>
 /// <param name="cache">The cache.</param>
 /// <param name="tag">The tag.</param>
 /// <param name="header">The header.</param>
 /// <returns></returns>
 public object Invoke(IServiceCache cache, object tag, CacheItemHeader header)
 {
     return(_invokeInternalInfo.MakeGenericMethod(Action.TType).Invoke(this, new[] { cache, tag, header }));
 }
コード例 #24
0
 public HomeController(IServiceCache serviceCache, IUserService userService)
 {
     _serviceCache = serviceCache;
     _userService  = userService;
 }
コード例 #25
0
 public CachePersister(ILogger log, IServiceCache cache)
 {
     Log    = log;
     _cache = cache;
 }
コード例 #26
0
        public void WorkingWithCache()
        {
            object        tag   = null;
            IServiceCache cache = null;

            // setting an item in cache
            cache["name"] = "value";
            // getting an item from cache
            var value = cache["name"];

            // adding an item to cache
            cache.Add("name", "value");
            // adding an item to cache with a CacheItemPolicy
            cache.Add("name", new CacheItemPolicy {
                SlidingExpiration = new TimeSpan(1, 0, 0)
            }, "value");
            // adding an item to cache with a tag
            cache.Add(tag, "name", "value");
            // adding an item to cache with a tag, and a CacheItemPolicy
            cache.Add(tag, "name", new CacheItemPolicy {
                SlidingExpiration = new TimeSpan(1, 0, 0)
            }, "value");

            // getting an item from cache
            var getValue = cache.Get("name");
            // getting an item from cache with a tag
            var getValue2 = cache.Get(tag, "name");
            // getting an item from cache
            var getValues = cache.Get(new[] { "name", "name2" });
            // getting an item from cache with a tag
            var getValues2 = cache.Get(tag, new[] { "name", "name2" });
            // trying to get an item from cache
            var hasValue = cache.TryGet("name", out value);
            // trying to get an item from cache with a tag
            var hasValue2 = cache.TryGet(tag, "name", out value);

            // getting a registered item from cache
            var registeredGetValue = (string)cache.Get(MyCache.MyItem);
            // getting a registered item from cache with values
            var registeredGetValue2 = (IEnumerable <string>)cache.Get(MyCache.MyItems2, new object[] { 10 });
            // getting a registered item from cache with a tag
            var registeredGetValue3 = (string)cache.Get(MyCache.MyItem, tag);
            // getting a registered item from cache with a tag, and values
            var registeredGetValue4 = (IEnumerable <string>)cache.Get(MyCache.MyItems2, tag, new object[] { 10 });
            // getting a registered item by name from cache
            var registeredGetValue5 = (string)cache.Get(typeof(MyCache), "MyItem");
            // getting a registered item by name from cache with values
            var registeredGetValue6 = (IEnumerable <string>)cache.Get(typeof(MyCache), "MyItems2", new object[] { 10 });
            // getting a registered item by name from cache with a tag
            var registeredGetValue7 = (string)cache.Get(typeof(MyCache), "MyItem", tag);
            // getting a registered item by name from cache with a tag, and values
            var registeredGetValue8 = (IEnumerable <string>)cache.Get(typeof(MyCache), "MyItems2", tag, new object[] { 10 });

            // getting a registered item from cache
            var registered2GetValue = cache.Get <string>(MyCache.MyItem);
            // getting a registered item from cache with values
            var registered2GetValue2 = cache.Get <IEnumerable <string> >(MyCache.MyItems2, new object[] { 10 });
            // getting a registered item from cache with a tag
            var registered2GetValue3 = cache.Get <string>(MyCache.MyItem, tag);
            // getting a registered item from cache with a tag, and values
            var registered2GetValue4 = cache.Get <IEnumerable <string> >(MyCache.MyItems2, tag, new object[] { 10 });
            // getting a registered item by name from cache
            var registered2GetValue5 = cache.Get <string>(typeof(MyCache), "MyItem");
            // getting a registered item by name from cache with values
            var registered2GetValue6 = cache.Get <IEnumerable <string> >(typeof(MyCache), "MyItems2", new object[] { 10 });
            // getting a registered item by name from cache with a tag
            var registered2GetValue7 = cache.Get <string>(typeof(MyCache), "MyItem", tag);
            // getting a registered item by name from cache with a tag, and values
            var registered2GetValue8 = cache.Get <IEnumerable <string> >(typeof(MyCache), "MyItems2", tag, new object[] { 10 });

            // getting a registered item from cache
            var registered3GetValue = cache.GetMany <string>(MyCache.MyItems);
            // getting a registered item from cache with values
            var registered3GetValue2 = cache.GetMany <string>(MyCache.MyItems2, new object[] { 10 });
            // getting a registered item from cache with a tag
            var registered3GetValue3 = cache.GetMany <string>(MyCache.MyItems, tag);
            // getting a registered item from cache with a tag, and values
            var registered3GetValue4 = cache.GetMany <string>(MyCache.MyItems2, tag, new object[] { 10 });
            // getting a registered item by name from cache
            var registered3GetValue5 = cache.GetMany <string>(typeof(MyCache), "MyItems");
            // getting a registered item by name from cache with values
            var registered3GetValue6 = cache.GetMany <string>(typeof(MyCache), "MyItems2", new object[] { 10 });
            // getting a registered item by name from cache with a tag
            var registered3GetValue7 = cache.GetMany <string>(typeof(MyCache), "MyItems", tag);
            // getting a registered item by name from cache with a tag, and values
            var registered3GetValue8 = cache.GetMany <string>(typeof(MyCache), "MyItems2", tag, new object[] { 10 });

            // getting a registered item from cache
            var registered4GetValue = cache.GetQuery <string>(MyCache.MyItemsQuery);
            // getting a registered item from cache with values
            var registered4GetValue2 = cache.GetQuery <string>(MyCache.MyItemsQuery, new object[] { 10 });
            // getting a registered item from cache with a tag
            var registered4GetValue3 = cache.GetQuery <string>(MyCache.MyItemsQuery, tag);
            // getting a registered item from cache with a tag, and values
            var registered4GetValue4 = cache.GetQuery <string>(MyCache.MyItemsQuery, tag, new object[] { 10 });
            // getting a registered item by name from cache
            var registered4GetValue5 = cache.GetQuery <string>(typeof(MyCache), "MyItemsQuery");
            // getting a registered item by name from cache with values
            var registered4GetValue6 = cache.GetQuery <string>(typeof(MyCache), "MyItemsQuery", new object[] { 10 });
            // getting a registered item by name from cache with a tag
            var registered4GetValue7 = cache.GetQuery <string>(typeof(MyCache), "MyItemsQuery", tag);
            // getting a registered item by name from cache with a tag, and values
            var registered4GetValue8 = cache.GetQuery <string>(typeof(MyCache), "MyItemsQuery", tag, new object[] { 10 });


            // removing an item from cache
            var removedValue = cache.Remove("name");
            // removing an item from cache with a tag
            var removedValue2 = cache.Remove(tag, "name");

            // removing a registered item from cache
            cache.Remove(MyCache.MyItem);
            // removing a registered item by name from cache
            cache.Remove(typeof(MyCache), "MyItem");

            // removing all registered items by anchorType from cache
            cache.RemoveAll(typeof(MyCache));

            // inserting an item into cache
            cache.Set("name", "value");
            // inserting an item into cache with a CacheItemPolicy
            cache.Set("name", new CacheItemPolicy {
                SlidingExpiration = new TimeSpan(1, 0, 0)
            }, "value");
            // inserting an item into cache with a tag
            cache.Set(tag, "name", "value");
            // inserting an item into cache with a tag, and a CacheItemPolicy and a name
            cache.Set(tag, "name", new CacheItemPolicy {
                SlidingExpiration = new TimeSpan(1, 0, 0)
            }, "value");

            // ensuring a cache dependency is ready from a builder
            ServiceCacheExtensions.EnsureCacheDependency(cache, tag, (c, r, t, v) => new[] { "tag", "tag2" });
            // ensuring a cache dependency is ready from values
            ServiceCacheExtensions.EnsureCacheDependency(cache, new[] { "tag", "tag2" });

            // touch cache tags
            cache.Touch(new[] { "tag", "tag2" });
            // touch cache tags with tag
            cache.Touch(tag, new[] { "tag", "tag2" });

            // wrapping a servicecache with a namespace
            var newCache2 = cache.BehaveAs("namespace");
            // wrapping a servicecache using a generated namespace from an object[] of values
            string @namespace;
            var    newCache = cache.BehaveAs(new object[] { "value", 5 }, out @namespace);
        }
コード例 #27
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MockServiceContext"/> class.
 /// </summary>
 /// <param name="request">The HTTP request.</param>
 /// <param name="response">The HTTP response.</param>
 /// <param name="cache">The service cache.</param>
 public MockServiceContext(IHttpRequest request, IHttpResponse response, IServiceCache cache) : base(request, response, cache)
 {
 }