private static string GetCacheItemKey(Type objectType, ObjectCacheAttribute cachingAttribute)
        {
            //determine entry key
            var key = string.Format("{0}.{1}", objectType.Namespace, objectType.Name);

            if (cachingAttribute.Scope == CacheScope.Group)
            {
                var group = Csla.ApplicationContext.ClientContext[CacheGroup];
                if (group == null)
                {
                    throw new Exception("ClientContext group required for Group scope data caching");
                }
                key = string.Format("{0}::{1}", key, group);
            }
            else if (cachingAttribute.Scope == CacheScope.User)
            {
                var group = Csla.ApplicationContext.ClientContext[CacheGroup];
                if (group == null)
                {
                    group = string.Empty;                //allow user scope with or without a specified grouping
                }
                var user = Csla.ApplicationContext.User;
                if (!user.Identity.IsAuthenticated)
                {
                    throw new Exception("Authenticated user required for User scope data caching");
                }
                key = string.Format("{0}::{1}::{2}", key, group, user.Identity.Name);
            }

            return(key);
        }
        public async Task <DataPortalResult> Fetch(Type objectType, object criteria, DataPortalContext context, bool isSync)
        {
            var cachingAttribute = ObjectCacheAttribute.GetObjectCacheAttribute(objectType);
            var cacheProvider    = CacheManager.GetCacheProvider();

            if (cachingAttribute != null && cacheProvider != null)
            {
                //get scenario details
                var scope           = cachingAttribute.Scope;
                var cacheByCriteria = cachingAttribute.CacheByCriteria;
                var expiration      = cachingAttribute.Expiration;

                //get key
                var key = GetCacheItemKey(objectType, cachingAttribute);

                //include criteria hash if needed
                if (cachingAttribute.CacheByCriteria)
                {
                    key = string.Format("{0}::{1}", key, criteria.GetHashCode());
                }

                var data = cacheProvider.Get(key);
                if (data == null)
                {
                    //cache miss
                    proxy = GetDataPortalProxy();
                    var results = await proxy.Fetch(objectType, criteria, context, isSync);

                    if (expiration > 0)
                    {
                        cacheProvider.Put(key, results.ReturnObject, new TimeSpan(0, expiration, 0));
                    }
                    else
                    {
                        cacheProvider.Put(key, results.ReturnObject);
                    }

                    return(results);
                }
                else
                {
                    //cache hit
#if !NET45 && !NET46
                    await TaskEx.Delay(0);
#else
                    await Task.Delay(0);
#endif
                    return(new DataPortalResult(data));
                }
            }
            else
            {
                proxy = GetDataPortalProxy();
                return(await proxy.Fetch(objectType, criteria, context, isSync));
            }
        }
Exemple #3
0
        public DataPortalResult Fetch(Type objectType, object criteria, DataPortalContext context)
        {
            var cachingAttribute = ObjectCacheAttribute.GetObjectCacheAttribute(objectType);
            var cacheProvider    = CacheManager.GetCacheProvider();

            if (cachingAttribute != null && cacheProvider != null)
            {
                //get scenario details
                var scope           = cachingAttribute.Scope;
                var cacheByCriteria = cachingAttribute.CacheByCriteria;
                var expiration      = cachingAttribute.Expiration;

                //get key
                var key = GetCacheItemKey(objectType, cachingAttribute);

                //include criteria hash if needed
                if (cachingAttribute.CacheByCriteria)
                {
                    key = string.Format("{0}::{1}", key, criteria.GetHashCode());
                }

                var data = cacheProvider.Get(key);
                if (data == null)
                {
                    //cache miss
                    proxy = GetDataPortalProxy();
                    var results = proxy.Fetch(objectType, criteria, context);
                    if (expiration > 0)
                    {
                        cacheProvider.Put(key, results, new TimeSpan(0, expiration, 0));
                    }
                    else
                    {
                        cacheProvider.Put(key, results);
                    }

                    return(results);
                }
                else
                {
                    //cache hit
                    return((DataPortalResult)data);
                }
            }
            else
            {
                proxy = GetDataPortalProxy();
                return(proxy.Fetch(objectType, criteria, context));
            }
        }
        private static string GetCacheItemKey(Type objectType, ObjectCacheAttribute cachingAttribute)
        {
            //determine entry key
            var key = string.Format("{0}.{1}", objectType.Namespace, objectType.Name);
            if (cachingAttribute.Scope == CacheScope.Group)
            {
                var group = Csla.ApplicationContext.ClientContext[CacheGroup];
                if (group == null) throw new ApplicationException("ClientContext group required for Group scope data caching");
                key = string.Format("{0}::{1}", key, group);
            }
            else if (cachingAttribute.Scope == CacheScope.User)
            {
                var group = Csla.ApplicationContext.ClientContext[CacheGroup];
                if (group == null) group = string.Empty; //allow user scope with or without a specified grouping
                var user = Csla.ApplicationContext.User;
                if (!user.Identity.IsAuthenticated) throw new ApplicationException("Authenticated user required for User scope data caching");
                key = string.Format("{0}::{1}::{2}", key, group, user.Identity.Name);
            }

            return key;
        }
Exemple #5
0
 /// <summary>
 /// Test cache system configuration and type declaration
 /// </summary>
 /// <param name="objectType">Target business object type</param>
 /// <returns>True if configuration is valid and type has ObjectCacheAttribute; else false</returns>
 public static bool IsCacheConfigured(Type objectType)
 {
     return(ObjectCacheAttribute.GetObjectCacheAttribute(objectType) != null && GetCacheProvider() != null);
 }