/// <summary> /// Gets the serialized objects stored at the given cache keys from the cache. /// </summary> /// <param name="cacheKeys">The cache keys.</param> /// <returns>A list of the serialized objects.</returns> public List <byte[]> Get(IEnumerable <string> cacheKeys) { // Sanitize if (cacheKeys == null) { return(null); } var result = new List <byte[]>(10); // Iterate all cache keys foreach (var cacheKey in cacheKeys) { // Sanitize if (string.IsNullOrWhiteSpace(cacheKey)) { // Skip continue; } // Try to get value var getResult = _memCache.Get(cacheKey); if (getResult != null) { result.Add(getResult); } } // Return the result return(result); }
/// <summary> /// Read Through Cache /// </summary> /// <param name="id"></param> /// <returns></returns> private MarkerInfoResponse GetMarkerInfoResponse(int uid, string pointType = null) { try { var cacheKey = CacheKeys.GetMarkerInfo(uid); var reply = _memCache.Get <MarkerInfoResponse>(cacheKey); if (reply != null) { // return cached data reply.IsCached = true; return(reply); } MapPoint marker = _pointCollection.Get(pointType).SingleOrDefault(i => i.MarkerId == uid); reply = new MarkerInfoResponse { Id = uid.ToString() }; reply.BuildContent(marker); if (GmcConfiguration.Get.CacheServices) { _memCache.Set(cacheKey, reply, TimeSpan.FromMinutes(10)); // cache data } return(reply); } catch (Exception ex) { return(new MarkerInfoResponse { OperationResult = "0", ErrorMessage = string.Format("MapService says: Parsing error param: {0}", ex.Message) }); } }
public PointsDatabase(IMemCache memCache, IList <MapPoint> points) { _memCache = memCache; Points = _memCache.Get <IList <MapPoint> >(CacheKeys.PointsDatabase); if (Points != null) { return; // cache hit } lock (_threadsafe) { //Points = _memCache.Get<IList<MapPoint>>(CacheKeys.PointsDatabase); //if (Points != null) return; Points = new List <MapPoint>(); // Not important, can be deleted, only for ensuring visual randomness of marker display // when not all can be displayed on screen // // Randomize order, when limit take is used for max marker display // random locations are selected // http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle var rnd = new Random(); var count = points.Count; for (var i = 0; i < count; i++) { MapPoint tmp = points[i]; int r = rnd.Next(count); points[i] = points[r]; points[r] = tmp; } Points = points; _memCache.Set <IList <MapPoint> >(CacheKeys.PointsDatabase, Points, TimeSpan.FromHours(24)); var data = _memCache.Get <IList <MapPoint> >(CacheKeys.PointsDatabase); if (data == null) { throw new Exception("cache not working"); } } }
/// <summary> /// Gets a byte array from the cache. /// </summary> /// <param name="key">The key of the byte array.</param> /// <returns> /// The byte array if found, otherwise null. /// </returns> /// <remarks> /// Return byte array will be decompressed via <see cref="GZipStream"/> before being returned. /// </remarks> public byte[] Get(string key) { // Sanitize if (string.IsNullOrWhiteSpace(key)) { return(null); } return(Decompress(_memCache.Get(key))); }
public PointsDatabase(IMemCache memCache, IList<MapPoint> points) { _memCache = memCache; Points = _memCache.Get<IList<MapPoint>>(CacheKeys.PointsDatabase); if (Points != null) return; // cache hit lock (_threadsafe) { //Points = _memCache.Get<IList<MapPoint>>(CacheKeys.PointsDatabase); //if (Points != null) return; Points = new List<MapPoint>(); // Not important, can be deleted, only for ensuring visual randomness of marker display // when not all can be displayed on screen // // Randomize order, when limit take is used for max marker display // random locations are selected // http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle var rnd = new Random(); var count = points.Count; for (var i = 0; i < count; i++) { MapPoint tmp = points[i]; int r = rnd.Next(count); points[i] = points[r]; points[r] = tmp; } Points = points; _memCache.Set<IList<MapPoint>>(CacheKeys.PointsDatabase, Points, TimeSpan.FromHours(24)); var data = _memCache.Get<IList<MapPoint>>(CacheKeys.PointsDatabase); if (data == null) { throw new Exception("cache not working"); } } }
public IList <MapPoint> Get(string pointType = null) { var key = GetPointKey(pointType); return(_memCache.Get <IList <MapPoint> >(key)); }
public PointsDatabase(IMemCache memCache, string filepath) { _memCache = memCache; Points = _memCache.Get<IList<P>>(CacheKeys.PointsDatabase); if (Points != null) return; // cache hit lock (_threadsafe) { Points = _memCache.Get<IList<P>>(CacheKeys.PointsDatabase); if (Points != null) return; var sw = new Stopwatch(); sw.Start(); Points = new List<P>(); FilePath = filepath; // Load from file List<P> points = Utility.Dataset.LoadDataset(FilePath); if (points.None()) { throw new Exception(string.Format("Data was not loaded from file: {0}", FilePath)); } if (points.Count > GmcSettings.Get.MaxPointsInCache) { points = points.Take(GmcSettings.Get.MaxPointsInCache).ToList(); } // Not important, can be deleted, only for ensuring visual randomness of marker display // when not all can be displayed on screen // // Randomize order, when limit take is used for max marker display // random locations are selected // http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle var rand = new Random(); var c = points.Count; for (var i = 0; i < c; i++) { P temp = points[i]; int r = rand.Next(c); points[i] = points[r]; points[r] = temp; } var linkedList = new LinkedList<P>(); points.ForEach(p => linkedList.AddLast(p)); Points = points; _memCache.Set<IList<P>>(Points, CacheKeys.PointsDatabase, TimeSpan.FromHours(24)); var data = _memCache.Get<IList<P>>(CacheKeys.PointsDatabase); if (data == null) { throw new Exception("cache not working"); } sw.Stop(); LoadTime = sw.Elapsed; } }
/// <summary> /// Read Through Cache /// </summary> /// <param name="input"></param> /// <returns></returns> public JsonMarkersReply GetMarkersHelper(JsonGetMarkersInput input) { try { var nelat = Math.Round(input.nelat.ToDouble(), Numbers.Round); var nelon = Math.Round(input.nelon.ToDouble(), Numbers.Round); var swlat = Math.Round(input.swlat.ToDouble(), Numbers.Round); var swlon = Math.Round(input.swlon.ToDouble(), Numbers.Round); var zoomLevel = int.Parse(input.zoomLevel); var filter = input.filter ?? ""; // values are validated there var inputValidated = new JsonGetMarkersReceive(nelat, nelon, swlat, swlon, zoomLevel, filter); var grid = GridCluster.GetBoundaryExtended(inputValidated); var cacheKeyHelper = string.Format("{0}_{1}_{2}", inputValidated.Zoomlevel, inputValidated.FilterHashCode(), grid.GetHashCode()); var cacheKey = CacheKeys.GetMarkers(cacheKeyHelper.GetHashCode()); var reply = _memCache.Get <JsonMarkersReply>(cacheKey); if (reply != null) { // return cached data reply.Cache = true; return(reply); } inputValidated.Viewport.ValidateLatLon(); // Validate google map viewport input (should be always valid) inputValidated.Viewport.Normalize(); // Get all points from memory IList <P> points = _pointsDatabase.GetPoints(); #region fiter // Filter points points = FilterUtil.FilterByType( points, new FilterData { TypeFilterExclude = inputValidated.TypeFilterExclude } ); #endregion filter // Create new instance for every ajax request with input all points and json data ICluster clusterAlgo = new GridCluster(points, inputValidated); var clusteringEnabled = inputValidated.IsClusteringEnabled || GmcSettings.Get.AlwaysClusteringEnabledWhenZoomLevelLess > inputValidated.Zoomlevel; // Clustering if (clusteringEnabled && inputValidated.Zoomlevel < GmcSettings.Get.ZoomlevelClusterStop) { #region cluster IList <P> markers = clusterAlgo.RunCluster(); #endregion cluster reply = new JsonMarkersReply { Markers = markers, Polylines = clusterAlgo.GetPolyLines(), }; } else { // If we are here then there are no clustering // The number of items returned is restricted to avoid json data overflow IList <P> filteredDataset = FilterUtil.FilterDataByViewport(points, inputValidated.Viewport); IList <P> filteredDatasetMaxPoints = filteredDataset.Take(GmcSettings.Get.MaxMarkersReturned).ToList(); reply = new JsonMarkersReply { Markers = filteredDatasetMaxPoints, Polylines = clusterAlgo.GetPolyLines(), Mia = filteredDataset.Count - filteredDatasetMaxPoints.Count, }; } // if client ne and sw is inside a specific grid box then cache the grid box and the result // next time test if ne and sw is inside the grid box and return the cached result if (GmcSettings.Get.CacheServices) { _memCache.Set(reply, cacheKey, TimeSpan.FromMinutes(10)); // cache data } return(reply); } catch (Exception ex) { return(new JsonMarkersReply { Ok = "0", EMsg = string.Format("MapService says: exception {0}", ex.Message) }); } }
public PointsDatabase(IMemCache memCache, string filepath) { _memCache = memCache; Points = _memCache.Get <IList <P> >(CacheKeys.PointsDatabase); if (Points != null) { return; // cache hit } lock (_threadsafe) { Points = _memCache.Get <IList <P> >(CacheKeys.PointsDatabase); if (Points != null) { return; } var sw = new Stopwatch(); sw.Start(); Points = new List <P>(); FilePath = filepath; // Load from file List <P> points = Utility.Dataset.LoadDataset(FilePath); if (points.None()) { throw new Exception(string.Format("Data was not loaded from file: {0}", FilePath)); } if (points.Count > GmcSettings.Get.MaxPointsInCache) { points = points.Take(GmcSettings.Get.MaxPointsInCache).ToList(); } // Not important, can be deleted, only for ensuring visual randomness of marker display // when not all can be displayed on screen // // Randomize order, when limit take is used for max marker display // random locations are selected // http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle var rand = new Random(); var c = points.Count; for (var i = 0; i < c; i++) { P temp = points[i]; int r = rand.Next(c); points[i] = points[r]; points[r] = temp; } var linkedList = new LinkedList <P>(); points.ForEach(p => linkedList.AddLast(p)); Points = points; _memCache.Set <IList <P> >(Points, CacheKeys.PointsDatabase, TimeSpan.FromHours(24)); var data = _memCache.Get <IList <P> >(CacheKeys.PointsDatabase); if (data == null) { throw new Exception("cache not working"); } sw.Stop(); LoadTime = sw.Elapsed; } }