private byte[] RefreshHierarchyPostCache(List<HierarchyPost> hierarchyPosts) { var serializedPosts = JsonConvert.SerializeObject(hierarchyPosts, new JsonSerializerSettings {DefaultValueHandling = DefaultValueHandling.Ignore}); var gzippedPosts = Gzipper.Zip(serializedPosts); var root = hierarchyPosts.First(); if (root.RootId != root.PostId) { throw new ArgumentException($"Can't refresh cache for Hierarchy post because the first post has value of RootId {root.RootId} and the PostId is {root.PostId}"); } var key = root.RootId.ToString(); var lastChangeTicks = DateTime.UtcNow.Ticks; var entries = new[] { new HashEntry("Posts", gzippedPosts), new HashEntry("LastChangeTicks", lastChangeTicks), new HashEntry("Views", root.Views) }; _redis.GetDatabase().HashSet(key, entries, CommandFlags.FireAndForget); var memoryKeyValue = new Tuple<PostMetadata, byte[]>( new PostMetadata { LastChangeTicks = lastChangeTicks, ViewsCount = root.Views }, gzippedPosts); _memoryCache.Set(key, memoryKeyValue); return gzippedPosts; }
/// <summary> /// Finds some book depending on function /// </summary> /// <param name="function">Condition to find book</param> /// <returns>Book</returns> public Book FindBookByTag(Func<Book, bool> function) { Book result = null; try { if (function == null) { logger.Error("Error while find book by tag"); throw new ArgumentNullException("Tag is null"); } books = repository.LoadToList(); result = books.First(function); logger.Info("Book was found successfully"); } catch (Exception e) { logger.Info(e.Message); logger.Error(e.StackTrace); } return result; }
public IEnumerable<KeyValuePair<int, string>> Search(string customerName) { if (string.IsNullOrEmpty(customerName) || string.IsNullOrWhiteSpace(customerName)) { yield return new KeyValuePair<int, string>(); } var search = new CustomerSearch(new LevenshteinDistance()); IEnumerable<KeyValuePair<int, string>> values = new List<KeyValuePair<int, string>>(); if (_customerRespository.Persistence == Persistence.SQL) { values = _customerRespository.All.Select(i => new KeyValuePair<int, string>(i.Id, i.Name)).ToList(); } else if (_customerRespository.Persistence == Persistence.Graph) { var graph = _customerRespository.GraphClient; values = graph.Cypher.Match("(p:Customer)").Return(p => p.As<CustomerGraph.Node>()).Results .Select(p => new KeyValuePair<int, string>((int)p.Id, p.Name)); } foreach (var match in search.FindClosestMatches(customerName, values.Select(i => i.Value), 5)) { yield return (new KeyValuePair<int, string>(values.First(c => c.Value == match).Key, match)); } }
private static Double[] CalculateResultVector(List<Double[]> vectors) { List<Double> resultVector = new List<Double>(); Int32 n = vectors.First().Length; for (Int32 i = 0; i < n; i++) { Double tmp = 0; for (Int32 j = 0; j < vectors.Count; j++) { tmp += vectors.ToArray()[j][i]; } resultVector.Add(tmp / vectors.Count); } return resultVector.ToArray(); }