Пример #1
0
        public T Get <T>(string key, long secondsToLive, Func <T> command)
        {
            if (_clientCacheProvider == null)
            {
                throw new Exception("DataCacheProvider is not set. Run DataCacheConfig.Initialize in Application_Start()");
            }

            DateTime entryDate;
            DateTime serverDate;
            long     fetchedSecondsToLive;

            T result;

            // Try to get the data
            if (_clientCacheProvider.TryGet <T>(key, out entryDate, out serverDate, out fetchedSecondsToLive, out result))
            {
                // Looks like we have data!
                // If the data is expired, but we got it back from the server, the Purge process hasn't picked up on it yet.
                // Let's go ahead and return what we have, but silently refetch the data in the background.
                if (entryDate.AddSeconds(secondsToLive) < serverDate)
                {
                    // If it'S REALLY old, and for some reason our Purge didn't pick up on this, run it again.
                    if (entryDate.AddSeconds(secondsToLive * 5) >= serverDate)
                    {
                        Task.Run(() =>
                        {
                            _clientCacheProvider.Put <T>(key, secondsToLive, command());
                        });
                    }
                    else
                    {
                        result = default(T);
                    }
                }
            }

            // If we didn't get any data back
            if (result == null)
            {
                result = command();
                _clientCacheProvider.Put <T>(key, secondsToLive, result);
            }

            return(result);
        }