CacheKey() private static méthode

Caches the key.
private static CacheKey ( int id ) : string
id int The id.
Résultat string
Exemple #1
0
        /// <summary>
        /// Reads the device by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="configuredGroupTypes">The configured group types.</param>
        /// <returns></returns>
        public static KioskDevice Read(int id, List <int> configuredGroupTypes)
        {
            string cacheKey = KioskDevice.CacheKey(id);

            ObjectCache cache  = Rock.Web.Cache.RockMemoryCache.Default;
            KioskDevice device = cache[cacheKey] as KioskDevice;

            // If the kioskdevice is currently inactive, but has a next active time prior to now, force a refresh
            if (device != null && device.FilteredGroupTypes(configuredGroupTypes).Count > 0 && !device.HasLocations(configuredGroupTypes))
            {
                if (device.KioskGroupTypes.Select(g => g.NextActiveTime).Min().CompareTo(RockDateTime.Now) < 0)
                {
                    device = null;
                }
            }

            if (device != null)
            {
                return(device);
            }
            else
            {
                using (var rockContext = new RockContext())
                {
                    var campusLocations = new Dictionary <int, int>();
                    Rock.Web.Cache.CampusCache.All()
                    .Where(c => c.LocationId.HasValue)
                    .Select(c => new
                    {
                        CampusId   = c.Id,
                        LocationId = c.LocationId.Value
                    })
                    .ToList()
                    .ForEach(c => campusLocations.Add(c.CampusId, c.LocationId));

                    var deviceModel = new DeviceService(rockContext)
                                      .Queryable("Locations")
                                      .Where(d => d.Id == id)
                                      .FirstOrDefault();

                    if (deviceModel != null)
                    {
                        device = new KioskDevice(deviceModel);
                        foreach (Location location in deviceModel.Locations)
                        {
                            LoadKioskLocations(device, location, campusLocations, rockContext);
                        }

                        var cachePolicy = new CacheItemPolicy();
                        cachePolicy.AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(60);
                        cache.Set(cacheKey, device, cachePolicy);

                        return(device);
                    }
                }
            }

            return(null);
        }
Exemple #2
0
        /// <summary>
        /// Reads the device by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="configuredGroupTypes">The configured group types.</param>
        /// <returns></returns>
        public static KioskDevice Read(int id, List <int> configuredGroupTypes)
        {
            object obj = new object();

            obj = _locks.GetOrAdd(id, obj);

            lock ( obj )
            {
                string cacheKey = KioskDevice.CacheKey(id);

                ObjectCache cache  = Rock.Web.Cache.RockMemoryCache.Default;
                KioskDevice device = cache[cacheKey] as KioskDevice;

                if (device != null)
                {
                    return(device);
                }
                else
                {
                    using (var rockContext = new RockContext())
                    {
                        var campusLocations = new Dictionary <int, int>();
                        Rock.Web.Cache.CampusCache.All()
                        .Where(c => c.LocationId.HasValue)
                        .Select(c => new
                        {
                            CampusId   = c.Id,
                            LocationId = c.LocationId.Value
                        })
                        .ToList()
                        .ForEach(c => campusLocations.Add(c.CampusId, c.LocationId));

                        var deviceModel = new DeviceService(rockContext)
                                          .Queryable().AsNoTracking()
                                          .Where(d => d.Id == id)
                                          .FirstOrDefault();

                        if (deviceModel != null)
                        {
                            device = new KioskDevice(deviceModel);
                            foreach (Location location in deviceModel.Locations)
                            {
                                LoadKioskLocations(device, location, campusLocations, rockContext);
                            }

                            cache.Set(cacheKey, device, new CacheItemPolicy {
                                AbsoluteExpiration = DateTimeOffset.Now.Date.AddDays(1)
                            });

                            return(device);
                        }
                    }
                }
            }

            return(null);
        }
Exemple #3
0
        /// <summary>
        /// Reads the device by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="configuredGroupTypes">The configured group types.</param>
        /// <returns></returns>
        public static KioskDevice Read(int id, List <int> configuredGroupTypes)
        {
            string cacheKey = KioskDevice.CacheKey(id);

            ObjectCache cache  = MemoryCache.Default;
            KioskDevice device = cache[cacheKey] as KioskDevice;

            // If the kioskdevice is currently inactive, but has a next active time prior to now, force a refresh
            if (device != null && device.FilteredGroupTypes(configuredGroupTypes).Count > 0 && !device.HasLocations(configuredGroupTypes))
            {
                if (device.KioskGroupTypes.Select(g => g.NextActiveTime).Min().CompareTo(DateTimeOffset.Now) < 0)
                {
                    device = null;
                }
            }

            if (device != null)
            {
                return(device);
            }
            else
            {
                var deviceModel = new DeviceService().Get(id);

                if (deviceModel != null)
                {
                    device = new KioskDevice(deviceModel);

                    using (new Rock.Data.UnitOfWorkScope())
                    {
                        foreach (Location location in deviceModel.Locations)
                        {
                            LoadKioskLocations(device, location);
                        }
                    }

                    var cachePolicy = new CacheItemPolicy();
                    cachePolicy.AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(60);
                    cache.Set(cacheKey, device, cachePolicy);

                    return(device);
                }
            }

            return(null);
        }
Exemple #4
0
        /// <summary>
        /// Flushes the specified id.
        /// </summary>
        /// <param name="id">The id.</param>
        public static void Flush(int id)
        {
            ObjectCache cache = MemoryCache.Default;

            cache.Remove(KioskDevice.CacheKey(id));
        }