예제 #1
0
 public FileCacheItem(string key, CacheItemPolicy policy = null, object payload = null)
 {
     Key = key;
     Policy = policy;
     Payload = payload;
 }
예제 #2
0
 public async void Basic_Counters()
 {
     string cacheName = "Basic_Counters_Test";
     var    cip       = new CacheItemPolicy() /* _absExpiry = ObjectCache.InfiniteAbsoluteExpiration */ }
예제 #3
0
 /// <summary>Inserts a cache entry into the cache. </summary>
 /// <param name="key">A unique identifier for the cache entry. </param>
 /// <param name="value">The object to insert.</param>
 /// <param name="policy">
 ///     An object that contains eviction details for the cache entry. This object provides more options
 ///     for eviction than a simple absolute expiration.
 /// </param>
 /// <param name="regionName">
 ///     Optional. A named region in the cache to which the cache entry can be added, if regions are
 ///     implemented. The default value for the optional parameter is null.
 /// </param>
 public override void Set(string key, object value, CacheItemPolicy policy, string regionName = null)
 {
     this.Set(new CacheItem(key, value, regionName), policy);
 }
예제 #4
0
 public MemoryCache(CacheItemPolicy policy)
 {
     _policy = policy;
 }
 protected ErzatsEnumBase(string CacheKey, CacheItemPolicy CachePolicy)
 {
     Cache.Add(CacheKey, this, CachePolicy, null);
 }
예제 #6
0
        public static T GetCache <T>(this ObjectCache objectCache, string cacheKey, Func <T> dataFunc, CacheItemPolicy cachePolicy)
            where T : class
        {
            T   data   = default(T);
            var cached = objectCache.Get(cacheKey);

            if (!(cached is CachedNullValue))
            {
                data = cached as T;
                if (data == null)
                {
                    data = dataFunc();
                    if (data == null)
                    {
                        objectCache.Add(cacheKey, CachedNullValue.Value, cachePolicy);
                    }
                    else
                    {
                        objectCache.Add(cacheKey, data, cachePolicy);
                    }
                }
            }
            return(data);
        }
 public static object GetItem(string key, CacheItemPolicy CIP)
 {
     return(GetOrAddExisting(key, () => InitItem(key), CIP));
 }
예제 #8
0
        public TcpBridge(string localIp, int tcpPortOne, int tcpPortMany)
        {
            OneIpAddress = IPAddress.Parse(localIp);
            OneListener  = new TcpListener(OneIpAddress, tcpPortOne);

            ManyIpAddress = IPAddress.Parse(localIp);
            ManyListener  = new TcpListener(ManyIpAddress, tcpPortMany);

            OneListener.Start();
            ManyListener.Start();

            Task.Run(() => OneAcceptConnections(Token), Token);
            Task.Run(() => ManyAcceptConnections(Token), Token);

            #region 1-->m 转发
            Task.Run(() =>
            {
                while (RunForever)
                {
                    try
                    {
                        if (!ManyQueue.TryDequeue(out var data))
                        {
                            Thread.Sleep(20);
                            continue;
                        }
                        ;

                        var policy = new CacheItemPolicy
                        {
                            AbsoluteExpiration = DateTime.Now.AddMinutes(10)
                        };
                        foreach (var cur in ManyClients)
                        {
                            ClientInfo.cache.Set("one", "", policy);
                            var key = cur.Value.TcpClient.Client.RemoteEndPoint.ToString();
                            if (!ManyClients.TryGetValue(key, out var value))
                            {
                                continue;
                            }

                            if (useLinkId)
                            {
                                if (ClientInfo.LinkId.HasValue && value.LinkId.HasValue)
                                {
                                    if (value.LinkId == ClientInfo.LinkId)
                                    {
                                        lock (value.SendLock)
                                        {
                                            value.NetworkStream.Write(data, 0, data.Length);
                                            value.NetworkStream.Flush();
                                        }
                                        $"[1->M]----[{ClientInfo.LinkId}-{key}]-{BytesToHexString(data)}----".Info();


                                        lock (ClientInfo.locker)
                                        {
                                            ClientInfo.IpAddress   = key;
                                            ClientInfo.RequestTime = DateTime.Now;
                                        }

                                        break;
                                    }
                                }
                            }
                            else
                            {
                                lock (value.SendLock)
                                {
                                    value.NetworkStream.Write(data, 0, data.Length);
                                    value.NetworkStream.Flush();

                                    $"[1->M]----已转发到[{key}]-{BytesToHexString(data)}".Info();
                                    ClientInfo.IpAddress   = key;
                                    ClientInfo.RequestTime = DateTime.Now;
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Logger.Error(e);
                        throw;
                    }
                }
            });
            #endregion

            #region m-->1 转发
            Task.Run(() =>
            {
                while (RunForever)
                {
                    try
                    {
                        if (!OneQueue.TryDequeue(out var data))
                        {
                            Thread.Sleep(100);
                            continue;
                        }
                        ;

                        foreach (var cur in OneClients)
                        {
                            var key = cur.Value.TcpClient.Client.RemoteEndPoint.ToString();

                            if (!OneClients.TryGetValue(key, out var value))
                            {
                                "One Client has removed!".Warn();
                                continue;
                            }

                            lock (value.SendLock)
                            {
                                value.NetworkStream.Write(data, 0, data.Length);
                                value.NetworkStream.Flush();
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Logger.Error(e);
                        //throw;
                    }
                }
            });
            #endregion
        }
 public override void Set(CacheItem item, CacheItemPolicy policy)
 {
     Set(item.Key, item.Value, policy, item.RegionName);
 }
예제 #10
0
 static Mem()
 {
     Cache       = MemoryCache.Default;
     CachePolicy = new CacheItemPolicy();
 }
예제 #11
0
        private static async Task ManyDataReceiver(ManyMetadata md)
        {
            var header = "[" + md.TcpClient.Client.RemoteEndPoint.ToString() + "]";

            $"{header} many data receiver started".Info();

            try
            {
                while (true)
                {
                    if (!IsClientConnected(md.TcpClient))
                    {
                        Logger.Error(header + $" client no longer connected[{md.LinkId}]");
                        break;
                    }

                    if (Token.IsCancellationRequested)
                    {
                        //if (isDebug)
                        Logger.Info(header + " cancellation requested");
                        break;
                    }
                    var data = await DataReadAsync(md.TcpClient, Token);

                    if (data == null || data.Length < 1)
                    {
                        Thread.Sleep(100);
                        continue;
                    }

                    var length = data.Length;

                    var linkId = 0;

                    var policy = new CacheItemPolicy
                    {
                        AbsoluteExpiration = DateTime.Now.AddMinutes(10)
                    };

                    switch (data.Length)
                    {
                    case 3:
                        linkId = data[2];

                        lock (ClientInfo.locker)
                        {
                            ClientInfo.cache.Set("many", "", policy);
                        }

                        switch (data[1])
                        {
                        case 0xC7:

                            $"收到连接[3][{ManyClients.Count}][{linkId}]注册包".Info();
                            md.LinkId = linkId;

                            var policy1 = new CacheItemPolicy
                            {
                                AbsoluteExpiration = DateTime.Now.AddSeconds(30 * 3)
                            };

                            lock (ClientInfo.locker)
                            {
                                ClientInfo.cache.Set(linkId.ToString(), "", policy1);
                            }
                            break;

                        case 0xC8:
                            //if (isDebug)
                        {
                            Logger.Info($"收到连接[3][{ManyClients.Count}][{linkId}]心跳包");
                        }

                            md.LinkId = linkId;

                            var policy2 = new CacheItemPolicy
                            {
                                AbsoluteExpiration = DateTime.Now.AddSeconds(30 * 3)
                            };
                            lock (ClientInfo.locker)
                            {
                                ClientInfo.cache.Set(linkId.ToString(), "", policy2);
                            }
                            break;
                        }
                        break;

                    case 4:
                        var linkIdHigh = data[2];
                        var linkIdLow  = data[3];

                        linkId = (linkIdHigh << 8) | (linkIdLow);
                        lock (ClientInfo.locker)
                        {
                            ClientInfo.cache.Set("many", "", policy);
                        }

                        switch (data[1])
                        {
                        case 0xC7:

                            $"收到连接[4][{ManyClients.Count}][{linkId}]注册包".Info();
                            md.LinkId = linkId;

                            var policy1 = new CacheItemPolicy
                            {
                                AbsoluteExpiration = DateTime.Now.AddSeconds(30 * 3)
                            };
                            lock (ClientInfo.locker)
                            {
                                ClientInfo.cache.Set(linkId.ToString(), "", policy1);
                            }
                            break;

                        case 0xC8:
                            //if (isDebug)
                            Logger.Info($"收到连接[4][{ManyClients.Count}][{linkId}]心跳包");
                            md.LinkId = linkId;

                            var policy2 = new CacheItemPolicy
                            {
                                AbsoluteExpiration = DateTime.Now.AddSeconds(30 * 3)
                            };
                            lock (ClientInfo.locker)
                            {
                                ClientInfo.cache.Set(linkId.ToString(), "", policy2);
                            }
                            break;
                        }
                        break;

                    default:
                        if (length <= 5)
                        {
                            continue;
                        }
                        Logger.Info($"[M][{md.TcpClient.Client.RemoteEndPoint}]:{BytesToHexString(data)}");
                        var crc32 = data.Take(length - 2).ToArray().CRC16();
                        if (data[length - 2] == crc32[1] && data[length - 1] == crc32[0])
                        {
                            if (useLinkId)
                            {
                                if (md.TcpClient.Client.RemoteEndPoint.ToString() == ClientInfo.IpAddress)
                                {
                                    var dt = new DateTime();

                                    lock (ClientInfo.locker)
                                    {
                                        dt = ClientInfo.RequestTime;
                                    }
                                    var ts = DateTime.Now - dt;

                                    if (ts.TotalMilliseconds < modbusTimeout + 500)
                                    {
                                        lock (ClientInfo.locker)
                                        {
                                            if (!isUseStrictCheckMode || (ClientInfo.ExpectedType == data[1] && ClientInfo.ExpectedDataLen == data[2]))
                                            {
                                                OneQueue.Enqueue(data.Take(data.Length).ToArray());
                                                $"[M->1]{header}:{ BytesToHexString(data)}".Info();
                                            }
                                            else
                                            {
                                                Logger.Warn($"[M->1] [{ClientInfo.ExpectedType}][{ClientInfo.ExpectedDataLen}],{BytesToHexString(data)}");
                                            }
                                        }
                                    }
                                    else
                                    {
                                        Logger.Warn($"TimeOut[{ts.TotalMilliseconds}][M->1] [{ClientInfo.ExpectedType}][{ClientInfo.ExpectedDataLen}],{BytesToHexString(data)}");
                                    }
                                }
                                else
                                {
                                    var dt = new DateTime();

                                    lock (ClientInfo.locker)
                                    {
                                        dt = ClientInfo.RequestTime;
                                    }

                                    var ts = DateTime.Now - dt;

                                    if (ts.TotalMilliseconds < modbusTimeout)
                                    {
                                        //Logger.Error($"[M->?][{md.TcpClient.Client.RemoteEndPoint}] :[{ ClientInfo.ManyIpAddress}]");
                                        //$"[M->?]{header}:{ BytesToHexString(data)}".Info();
                                    }
                                    else
                                    {
                                        //Logger.Error("[M->?] timeout!");
                                    }
                                }
                            }
                            else
                            {
                                OneQueue.Enqueue(data.Take(data.Length).ToArray());
                                $"[M->1]{header}: { BytesToHexString(data)}".Info();
                            }
                        }
                        else
                        {
                            Logger.Error($"CRC Error:{crc32[1]}:{crc32[0]}");
                        }
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                Logger.Error(
                    Environment.NewLine +
                    header +
                    " ManyDataReceiver Exception: " +
                    Environment.NewLine +
                    e.ToString() +
                    Environment.NewLine +
                    e.StackTrace
                    );
            }
            //if (isDebug)
            Logger.Info(header + " data receiver terminating");

            ManyClients.TryRemove(md.TcpClient.Client.RemoteEndPoint.ToString(), out _);
            md.Dispose();
        }
예제 #12
0
        private static object GetCacheData(IContentQuery <TextContent> contentQuery, TakeOperation operation, CacheItemPolicy policy, Func <object> getData)
        {
            string cacheKey = "TakeOperation:" + operation.ToString() + " " + TextTranslator.Translate(contentQuery);
            var    data     = contentQuery.Repository.ObjectCache().Get(cacheKey);

            if (data == null)
            {
                data = getData();
                contentQuery.Repository.ObjectCache().AddOrGetExisting(cacheKey, data, policy);
            }
            return(data);
        }
예제 #13
0
        /// <summary>
        /// Set inserts or updates an existing cache entry with the given key.
        /// If eviction policy is null, a Sliding Expiration policy with five minute
        /// timespan will be used. VirtualCache doesn't support regions thus, regionName
        /// will be ignored and defaults to null.
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="policy"></param>
        /// <param name="regionName"></param>
        public override void Set(string key, object value, CacheItemPolicy policy, string regionName = null)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (policy == null)
            {
                policy = DefaultPolicy;
            }

            var now   = GetCurrentDate(0);
            var entry = new CacheEntry(policy, now)
            {
                Value = value
            };
            var cacheKey = new CacheKey(key)
            {
                TimeStamp = now
            };

            _store.Locker.Invoke(() =>
            {
                #region update block
                if (_store.AddIfNotExist(cacheKey, entry))
                {
                    return;
                }

                // if priotiry is not removable, just update the store w/ value
                if (entry.NonExpiring)
                {
                    _store.CurrentValue.Value = value;
                    return;
                }

                entry       = _store.CurrentValue;
                entry.Value = value;
                if (!entry.IsExpired(now))
                {
                    // slide the expire time...
                    if (entry.SlidingExpiration > 0)
                    {
                        entry.ExpirationTime = GetCurrentDate(entry.SlidingExpiration);
                        if (!_store.IsDataInKeySegment)
                        {
                            _store.CurrentValue = entry;
                        }
                    }
                    return;
                }
                // adjust policy (set to slide every 5 mins) of the expired item to accomodate this update...
                if (entry.SlidingExpiration == 0)
                {
                    entry.SlidingExpiration = TimeSpan.TicksPerMinute * 5;
                }
                entry.ExpirationTime = GetCurrentDate(entry.SlidingExpiration);
                if (!_store.IsDataInKeySegment)
                {
                    _store.CurrentValue = entry;
                }
                return;

                #region for removal
                //if (_store.AddIfNotExist(cacheKey, ce))
                //    return;
                //// update the store...
                //cacheKey.TimeStamp = _store.CurrentKey.TimeStamp;
                //_store.CurrentKey.TimeStamp = now;
                //var ce2 = new CacheEntry(policy, GetCurrentDate(0));
                //ce2.Value = value;
                //_store.CurrentValue = ce2;
                #endregion
                #endregion
            });
        }
예제 #14
0
 public bool Add(string key, object value, CacheItemPolicy policy, string regionName = null)
 {
     return(cache.Add(key, value, policy, regionName));
 }
예제 #15
0
		public void Set (string key, object value, CacheItemPolicy policy)
		{
			bool locked = false;
			try {
				cache_lock.EnterWriteLock ();
				locked = true;

				MemoryCacheEntry mce;
				bool update = false;
				if (cache.TryGetValue (key, out mce)) {
					if (mce != null) {
						perfCounters.Increment (MemoryCachePerformanceCounters.CACHE_HITS);
						mce.Value = value;
						mce.SetPolicy (policy);
						if (mce.IsExpirable)
							UpdateExpirable (mce);
						lru.Update (mce);
						return;
					}

					update = true;
				}
				perfCounters.Increment (MemoryCachePerformanceCounters.CACHE_MISSES);
				if (policy != null)
					mce = new MemoryCacheEntry (owner, key, value,
								    policy.AbsoluteExpiration,
								    policy.ChangeMonitors,
								    policy.Priority,
								    policy.RemovedCallback,
								    policy.SlidingExpiration,
								    policy.UpdateCallback);
				else
					mce = new MemoryCacheEntry (owner, key, value);
				AddToCache (key, mce, update);
			} finally {
				if (locked)
					cache_lock.ExitWriteLock ();
			}
		}
        public async Task <string> StartLivePriceSearchAsync(string country, string currency, string locale, string originPlaceSkyscannerCode, string destinationPlaceSkyscannerCode, DateTime outboundPartialDate, DateTime?inboundPartialDate, int adults, int children, int infants, string cabinClass, CancellationToken cancellationToken)
        {
            try
            {
                string outboundPartialDateString = outboundPartialDate.ToString("yyyy-MM-dd");
                string inboundPartialDateString  = "";
                if (inboundPartialDate.HasValue)
                {
                    inboundPartialDateString = inboundPartialDate.Value.ToString("yyyy-MM-dd");
                }

                List <object> cacheKeyList = new List <object>();
                cacheKeyList.Add("StartLivePriceSearchAsync");
                cacheKeyList.Add(country);
                cacheKeyList.Add(currency);
                cacheKeyList.Add(locale);
                cacheKeyList.Add("sky"); //Iata, GeoNameCode, GeoNameId, Rnid*, Sky, Latlong
                cacheKeyList.Add("on");
                cacheKeyList.Add(originPlaceSkyscannerCode);
                cacheKeyList.Add(destinationPlaceSkyscannerCode);
                cacheKeyList.Add(outboundPartialDateString);
                cacheKeyList.Add(inboundPartialDateString);
                cacheKeyList.Add(adults.ToString());
                cacheKeyList.Add(children.ToString());
                cacheKeyList.Add(infants.ToString());
                cacheKeyList.Add(cabinClass);

                string cacheKey = string.Join(":", cacheKeyList);

                string pollURL = (string)cache.Get(cacheKey);
                if (pollURL == null)
                {
                    var apiKey = DefaultApiKey();

                    var variablesURL = new Dictionary <string,
                                                       string>();
                    variablesURL.Add("{apiKey}", apiKey);
                    var URL = Settings.StartLivePricesURL.ReplaceFromDictionary(variablesURL);

                    var variables = new Dictionary <string,
                                                    string>();
                    variables.Add("country", country);
                    variables.Add("currency", currency);
                    variables.Add("locale", locale);
                    variables.Add("locationschema", "Sky"); //Iata, GeoNameCode, GeoNameId, Rnid*, Sky, Latlong
                    variables.Add("apikey", apiKey);
                    variables.Add("grouppricing", "true");  // doesn't work
                                                            //variables.Add("grouppricing", "off");
                    variables.Add("originplace", originPlaceSkyscannerCode);
                    variables.Add("destinationplace", destinationPlaceSkyscannerCode);
                    variables.Add("outbounddate", outboundPartialDate.ToString("yyyy-MM-dd"));
                    if (inboundPartialDate.HasValue)
                    {
                        variables.Add("inbounddate", inboundPartialDate.Value.ToString("yyyy-MM-dd"));
                    }
                    variables.Add("adults", adults.ToString());
                    variables.Add("children", children.ToString());
                    variables.Add("infants", infants.ToString());
                    variables.Add("cabinclass", cabinClass);


                    Func <Exception, Boolean> exception429 = ex => ex is SimpleHttpResponseException && (((int)((SimpleHttpResponseException)ex).StatusCode) == 429);
                    var response = await TaskHelper.DoAsync(() => WebRequestHelper.PostFormAsync(URL, variables, WebRequestHelper.ResponseAcceptType.JSON, cancellationToken), new AsyncRetryPolicy(TimeSpan.FromSeconds(Settings.Exception429WaitTime), Settings.Exception429Retries, exception429), cancellationToken);

                    pollURL = response.Item2.Location.AbsoluteUri + "?";

                    CacheItemPolicy policy = new CacheItemPolicy();
                    policy.AbsoluteExpiration = new DateTimeOffset(DateTime.Now.AddMinutes(Settings.StartLivePricesCacheExpiration));
                    cache.Add(cacheKey, pollURL, policy);
                }

                return(pollURL);
            }
            catch
            {
                throw;
            }
        }
예제 #17
0
 public T GetOrAdd <T>(string key, Func <T> addItemFactory, CacheItemPolicy policy)
 {
     return(addItemFactory.Invoke());
 }
        public async Task <LivePricesServiceResponse> PollLivePriceSearchAsync(string pollURL, string cacheKey, bool untilComplete, int adults, int children, int infants, int?maxStopsFilter, CancellationToken cancellationToken)
        {
            string responseString = "";

            try
            {
                LivePricesServiceResponse data = (LivePricesServiceResponse)cache.Get(cacheKey);
                if (data == null)
                {
                    var variables = new Dictionary <string,
                                                    string>();
                    variables.Add("apikey", DefaultApiKey());
                    variables.Add("adults", adults.ToString());
                    variables.Add("children", children.ToString());
                    variables.Add("infants", infants.ToString());

                    //can we change passengers and stops when polling?

                    //Return Schema
                    variables.Add("locationschema", "Iata"); //Iata, GeoNameCode, GeoNameId, Rnid*, Sky, Latlong

                    if (maxStopsFilter.HasValue)
                    {
                        //maxStops is actually stops filter
                        variables.Add("stops", maxStopsFilter.ToString());
                    }

                    //carrier, duration, outboundarrivetime, outbounddeparttime, inboundarrivetime, inbounddeparttime, price*
                    //variables.Add("sorttype", "price");
                    //asc, desc
                    //variables.Add("sortorder", "desc");
                    variables.Add("includeQuery", "true");

                    //variables.Add("includecarriers", "");
                    //variables.Add("excludecarriers", "");

                    var newPollURL = pollURL + variables.ToQueryString();

                    var complete = false;
                    while (!complete)
                    {
                        Func <Exception, Boolean> exception304 = ex => ex is SimpleHttpResponseException && (((int)((SimpleHttpResponseException)ex).StatusCode) == 304);
                        Func <Exception, Boolean> exception429 = ex => ex is SimpleHttpResponseException && (((int)((SimpleHttpResponseException)ex).StatusCode) == 429);
                        var response = await TaskHelper.DoAsync(() => TaskHelper.DoAsync(() => WebRequestHelper.GetAsync(newPollURL, WebRequestHelper.ResponseAcceptType.JSON, cancellationToken), new AsyncRetryPolicy(TimeSpan.FromSeconds(Settings.Exception304WaitTime), Settings.Exception304Retries, exception304), cancellationToken), new AsyncRetryPolicy(TimeSpan.FromSeconds(Settings.Exception429WaitTime), Settings.Exception429Retries, exception429), cancellationToken);

                        responseString = response.Item1.BytesToString();
                        data           = JsonConvert.DeserializeObject <LivePricesServiceResponse>(responseString);

                        if (data != null && ((data.Status == "UpdatesComplete") || (data.Status == "UpdatesPending" && !untilComplete)))
                        {
                            complete = true;
                        }
                    }

                    if (data.Places == null)
                    {
                        data.Places = new List <Place>();
                    }

                    if (data.Currencies == null)
                    {
                        data.Currencies = new List <Currency>();
                    }

                    CacheItemPolicy policy = new CacheItemPolicy();
                    policy.AbsoluteExpiration = new DateTimeOffset(DateTime.Now.AddMinutes(Settings.PollLivePricesCacheExpiration));
                    cache.Add(cacheKey, data, policy);
                }

                return(data);
            }
            catch (Newtonsoft.Json.JsonReaderException)
            {
                throw;
            }
            catch
            {
                throw;
            }
        }
예제 #19
0
 public SerializableCacheItemPolicy(CacheItemPolicy policy)
 {
     AbsoluteExpiration = policy.AbsoluteExpiration;
     SlidingExpiration  = policy.SlidingExpiration;
 }
        //Markets!
        public async Task <CountriesServiceResponse> GetCountriesByLocaleAsync(string locale, CancellationToken cancellationToken)
        {
            List <object> cacheKeyList = new List <object>();

            cacheKeyList.Add("GetCountriesByLocaleAsync");
            cacheKeyList.Add(locale);

            string cacheKey = string.Join(":", cacheKeyList);

            CountriesServiceResponse data = (CountriesServiceResponse)cache.Get(cacheKey);

            if (data == null && !cache.Contains(cacheKey))
            {
                if (!Settings.CountriesServiceLock.ContainsKey(locale))
                {
                    Settings.CountriesServiceLock.TryAdd(locale, new SemaphoreSlim(1, 1));
                }

                await Settings.CountriesServiceLock[locale].WaitAsync();
                try
                {
                    data = (CountriesServiceResponse)cache.Get(cacheKey);
                    if (data == null && !cache.Contains(cacheKey))
                    {
                        string responseString = "";

                        var variablesPath = new Dictionary <string, string>();
                        variablesPath.Add("{locale}", locale);

                        if (_dataPath != "" && File.Exists(_dataPath + Settings.CountriesServiceFileName.ReplaceFromDictionary(variablesPath)))
                        {
                            var path = _dataPath + Settings.CountriesServiceFileName.ReplaceFromDictionary(variablesPath);
                            responseString = File.ReadAllText(path);
                        }
                        else
                        {
                            var variables = new Dictionary <string, string>();
                            variables.Add("{apiKey}", ApiKey());
                            variables.Add("{locale}", locale);

                            var URL      = Settings.CountriesServiceURL.ReplaceFromDictionary(variables);
                            var response = await WebRequestHelper.GetAsync(URL, WebRequestHelper.ResponseAcceptType.JSON, cancellationToken);

                            responseString = response.Item1.BytesToString();

                            try
                            {
                                if (_dataPath != "")
                                {
                                    File.WriteAllText(_dataPath + Settings.CountriesServiceFileName.ReplaceFromDictionary(variablesPath), responseString);
                                }
                            }
                            catch
                            {
                            }
                        }

                        data = JsonConvert.DeserializeObject <CountriesServiceResponse>(responseString);

                        for (int i = 0; i < data.Countries.Count(); i++)
                        {
                            var country    = data.Countries[i];
                            var geoCountry = await GetCountryByIDAsync(country.Code, cancellationToken);

                            if (geoCountry != null)
                            {
                                country.CurrencyId = geoCountry.CurrencyId;
                            }
                            else
                            {
                            }
                        }

                        CacheItemPolicy policy = new CacheItemPolicy();
                        policy.AbsoluteExpiration = new DateTimeOffset(DateTime.Now.AddMinutes(Settings.CountriesServiceCacheExpiration));
                        cache.Add(cacheKey, data, policy);
                    }
                }
                finally
                {
                    Settings.CountriesServiceLock[locale].Release();
                }
            }

            return(data);
        }
예제 #21
0
        private void CacheResponse(string cacheKey, DateTime requestSendTime, Task <object> responseTask, IMethodCachingSettings settings)
        {
            var cacheItem = new AsyncCacheItem
            {
                NextRefreshTime = DateTime.UtcNow + settings.RefreshTime.Value,
                Value           = responseTask,
            };

            // Register each revoke key in the response with the reverse index, if the response is not an exception and is a Revocable<>.
            // Starting from now, the IsRevoked property in cacheItem might be set to true by another thread in OnRevoked().
            var revokeKeys = ExtarctRevokeKeys(responseTask);

            if (revokeKeys.Any())
            {
                lock (RevokeKeyToCacheItemsIndex)
                    foreach (var revokeKey in revokeKeys)
                    {
                        RevokeKeyToCacheItemsIndex.GetOrAdd(revokeKey, _ => new HashSet <AsyncCacheItem>()).Add(cacheItem);
                    }
            }

            // Check if we got a revoke message from one of the revoke keys in the response while the request was in progress.
            // Do this AFTER the above, so there's no point in time that we don't monitor cache revokes
            var recentlyRevokedKey = revokeKeys.FirstOrDefault(rk => RecentRevokesCache.TryGetRecentlyRevokedTime(rk, requestSendTime) != null);

            if (recentlyRevokedKey != null)
            {
                Items.Meter("RevokeRaceCondition", Unit.Items).Mark();
                Log.Warn(x => x("Got revoke during request, marking as stale", unencryptedTags: new
                {
                    revokeKey = recentlyRevokedKey,
                    requestSendTime,
                    cacheKey,
                    revokeTime = RecentRevokesCache.TryGetRecentlyRevokedTime(recentlyRevokedKey, requestSendTime)
                }));
                cacheItem.IsRevoked = true;
            }

            // Set the MemoryCache policy based on our caching settings. If the response has no revoke keys, no need to receive a callback
            // event when it's removed from the cache since we don't need to remove it from the reverse index.
            var cacheItemPolicy = new CacheItemPolicy {
            };

            if (settings.ExpirationBehavior == ExpirationBehavior.ExtendExpirationWhenReadFromCache)
            {
                cacheItemPolicy.SlidingExpiration = settings.ExpirationTime.Value;
            }
            else
            {
                cacheItemPolicy.AbsoluteExpiration = DateTime.UtcNow + settings.ExpirationTime.Value;
            }

            if (revokeKeys.Any())
            {
                cacheItemPolicy.RemovedCallback += ItemRemovedCallback;
            }

            // Store the response in the main cache. Note that this will trigger a removal of a currently-cached value, if any,
            // i.e. call ItemRemovedCallback() with Reason=Removed.
            MemoryCache.Set(new CacheItem(cacheKey, cacheItem), cacheItemPolicy);
        }
        public async Task <BrowseRoutesServiceResponse> BrowseRoutesSearchAsync(string country, string currency, string locale, string originPlaceSkyscannerCode, string destinationPlaceSkyscannerCode, DateTime?outboundPartialDate, DateTime?inboundPartialDate, CancellationToken cancellationToken)
        {
            string outboundPartialDateString = "";

            if (outboundPartialDate.HasValue)
            {
                outboundPartialDateString = outboundPartialDate.Value.ToString("yyyy-MM-dd");
            }
            string inboundPartialDateString = "";

            if (inboundPartialDate.HasValue)
            {
                inboundPartialDateString = inboundPartialDate.Value.ToString("yyyy-MM-dd");
            }

            List <object> cacheKeyList = new List <object>();

            cacheKeyList.Add("BrowseRoutesSearchAsync");
            cacheKeyList.Add(country);
            cacheKeyList.Add(currency);
            cacheKeyList.Add(locale);
            cacheKeyList.Add(originPlaceSkyscannerCode);
            cacheKeyList.Add(destinationPlaceSkyscannerCode);
            cacheKeyList.Add(outboundPartialDateString);
            cacheKeyList.Add(inboundPartialDateString);

            string cacheKey = string.Join(":", cacheKeyList);

            BrowseRoutesServiceResponse data = (BrowseRoutesServiceResponse)cache.Get(cacheKey);

            if (data == null)
            {
                var variables = new Dictionary <string,
                                                string>();
                variables.Add("{apiKey}", ApiKey());
                variables.Add("{market}", country);
                variables.Add("{currency}", currency);
                variables.Add("{locale}", locale);
                // If you exceed the rate limit, it will reset after 60 seconds.
                //Sessions expire after 30 minutes.
                //https://github.com/Skyscanner/api-documentation/blob/master/live_flights_pricing/README.md
                //37.678,-122.452-latlong
                //188.39.95.93-ip
                variables.Add("{originPlace}", originPlaceSkyscannerCode);
                variables.Add("{destinationPlace}", destinationPlaceSkyscannerCode);
                if (outboundPartialDate.HasValue)
                {
                    variables.Add("{outboundPartialDate}", outboundPartialDate.Value.ToString("yyyy-MM-dd"));
                }
                else
                {
                    variables.Add("{outboundPartialDate}", "anytime");
                }

                if (inboundPartialDate.HasValue)
                {
                    variables.Add("{inboundPartialDate}", inboundPartialDate.Value.ToString("yyyy-MM-dd"));
                }
                else
                {
                    variables.Add("{inboundPartialDate}", "");
                }

                var URL = Settings.PollBrowseRoutesURL.ReplaceFromDictionary(variables);
                Func <Exception, Boolean> exception429 = ex => ex is SimpleHttpResponseException && (((int)((SimpleHttpResponseException)ex).StatusCode) == 429);
                var response = await TaskHelper.DoAsync(() => WebRequestHelper.GetAsync(URL, WebRequestHelper.ResponseAcceptType.JSON, cancellationToken), new AsyncRetryPolicy(TimeSpan.FromSeconds(Settings.Exception429WaitTime), Settings.Exception429Retries, exception429), cancellationToken);

                data = JsonConvert.DeserializeObject <BrowseRoutesServiceResponse>(response.Item1.BytesToString());

                CacheItemPolicy policy = new CacheItemPolicy();
                policy.AbsoluteExpiration = new DateTimeOffset(DateTime.Now.AddMinutes(Settings.PollBrowseRoutesCacheExpiration));
                cache.Add(cacheKey, data, policy);
            }

            return(data);
        }
예제 #23
0
 public void Add <T>(string key, T value, CacheItemPolicy policy)
 {
     _cache.Add(key, value, policy);
 }
예제 #24
0
 public override CacheItem AddOrGetExisting(CacheItem value, CacheItemPolicy policy)
 {
     AddOrGetExistingInternal(value.Key, value.Value, policy);
     return(value);
 }
예제 #25
0
 public Repository(ICacheProvider cacheProvider, CacheItemPolicy policy)
     : this(new SmartContext(), cacheProvider, policy)
 {
 }
예제 #26
0
 public DeviceTypeMemoryCache(string region = null, CacheItemPolicy cacheItemPolicy = null) : base(region, cacheItemPolicy)
 {
 }
예제 #27
0
 /// <summary>Inserts a cache entry into the cache, specifying information about how the entry will be evicted.</summary>
 /// <returns>
 ///     true if the insertion try succeeds, or false if there is an already an entry in the cache with the same key as
 ///     <paramref name="key" />.
 /// </returns>
 /// <param name="key">A unique identifier for the cache entry. </param>
 /// <param name="value">The object to insert. </param>
 /// <param name="policy">
 ///     An object that contains eviction details for the cache entry. This object provides more options
 ///     for eviction than a simple absolute expiration.
 /// </param>
 /// <param name="regionName">
 ///     Optional. A named region in the cache to which the cache entry can be added, if regions are
 ///     implemented. The default value for the optional parameter is null.
 /// </param>
 public override bool Add(string key, object value, CacheItemPolicy policy, string regionName = null)
 {
     return(this.Add(new CacheItem(key, value, regionName), policy));
 }
예제 #28
0
        public void Set(string key, object data)
        {
            CacheItemPolicy policy = new CacheItemPolicy();

            Cache.Add(new CacheItem(key, data), policy);
        }
예제 #29
0
        public override object AddOrGetExisting(string key, object value, CacheItemPolicy policy, string regionName = null)
        {
            var o = AddOrGetExisting(new CacheItem(key, value, regionName), policy);

            return(o?.Value);
        }
예제 #30
0
 public bool SetCache(CacheItem item, CacheItemPolicy policy)
 {
     return(_cache.Add(item, policy));
 }
예제 #31
0
		public object AddOrGetExisting (string key, object value, CacheItemPolicy policy)
		{
			bool readLocked = false, writeLocked = false;
			try {
				cache_lock.EnterUpgradeableReadLock ();
				readLocked = true;

				MemoryCacheEntry entry;
				if (cache.TryGetValue (key, out entry) && entry != null) {
					perfCounters.Increment (MemoryCachePerformanceCounters.CACHE_HITS);
					return entry.Value;
				}
				perfCounters.Increment (MemoryCachePerformanceCounters.CACHE_MISSES);
				
				cache_lock.EnterWriteLock ();
				writeLocked = true;

				if (policy == null)
					entry = new MemoryCacheEntry (owner, key, value);
				else
					entry = new MemoryCacheEntry (owner, key, value,
								      policy.AbsoluteExpiration,
								      policy.ChangeMonitors,
								      policy.Priority,
								      policy.RemovedCallback,
								      policy.SlidingExpiration,
								      policy.UpdateCallback);

				AddToCache (key, entry);
				return null;
			} finally {
				if (writeLocked)
					cache_lock.ExitWriteLock ();
				if (readLocked)
					cache_lock.ExitUpgradeableReadLock ();
			}
		}
예제 #32
0
 public object AddOrGetExistingCache(CacheItem item, CacheItemPolicy policy)
 {
     return(_cache.AddOrGetExisting(item, policy));
 }
예제 #33
0
		public void SetPolicy (CacheItemPolicy policy)
		{
			if (policy == null)
				return;
			
			absoluteExpiration = policy.AbsoluteExpiration;
			monitors = policy.ChangeMonitors;
			priority = policy.Priority;
			removedCallback = policy.RemovedCallback;
			slidingExpiration = policy.SlidingExpiration;
			updateCallback = policy.UpdateCallback;
		}
예제 #34
0
 public void Add <T>(string key, T item, CacheItemPolicy policy)
 {
 }