Ejemplo n.º 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(); }
 }
Ejemplo n.º 2
0
        public IActionResult Get()
        {
            // TODO: Use GraphQL for this
            // https://github.com/graphql-dotnet/graphql-dotnet
            var cachedResponse = serviceCache.Get();

            if (cachedResponse == null)
            {
                var response = Ok(employeeService.Get());
                serviceCache.Add(response);
                return(response);
            }

            return(cachedResponse);
        }
Ejemplo n.º 3
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);
 }
Ejemplo n.º 4
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);
                        }
                    }
                }
            }
        }
 private object InvokeInternal <T>(IServiceCache cache, object tag, CacheItemHeader header)
 {
     return(ActionInvoke <T>(Message, tag, header.Values, () => (T)cache.Get(null, header.Item)));
 }
Ejemplo n.º 6
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);
        }