public void Cache_Null() { var source = GetRandomStringKeys(1); var loader = new CacheLoader <string, string>(source); var cache = new Cache <string, string>(source.Count, loader); Console.WriteLine(cache[null]); }
void Awake() { if (CacheLoader_Instance == null) { CacheLoader_Instance = this; } foldercheck(); }
/// <exception cref="System.IO.IOException"/> /// <exception cref="Sharpen.URISyntaxException"/> /// <exception cref="System.Exception"/> /// <exception cref="Sharpen.ExecutionException"/> public virtual void TestDownloadPublicWithStatCache() { Configuration conf = new Configuration(); FileContext files = FileContext.GetLocalFSFileContext(conf); Path basedir = files.MakeQualified(new Path("target", typeof(TestFSDownload).Name )); // if test directory doesn't have ancestor permission, skip this test FileSystem f = basedir.GetFileSystem(conf); Assume.AssumeTrue(FSDownload.AncestorsHaveExecutePermissions(f, basedir, null)); files.Mkdir(basedir, null, true); conf.SetStrings(typeof(TestFSDownload).FullName, basedir.ToString()); int size = 512; ConcurrentMap <Path, AtomicInteger> counts = new ConcurrentHashMap <Path, AtomicInteger >(); CacheLoader <Path, Future <FileStatus> > loader = FSDownload.CreateStatusCacheLoader (conf); LoadingCache <Path, Future <FileStatus> > statCache = CacheBuilder.NewBuilder().Build (new _CacheLoader_328(counts, loader)); // increment the count // use the default loader // test FSDownload.isPublic() concurrently int fileCount = 3; IList <Callable <bool> > tasks = new AList <Callable <bool> >(); for (int i = 0; i < fileCount; i++) { Random rand = new Random(); long sharedSeed = rand.NextLong(); rand.SetSeed(sharedSeed); System.Console.Out.WriteLine("SEED: " + sharedSeed); Path path = new Path(basedir, "test-file-" + i); CreateFile(files, path, size, rand); FileSystem fs = path.GetFileSystem(conf); FileStatus sStat = fs.GetFileStatus(path); tasks.AddItem(new _Callable_358(fs, path, sStat, statCache)); } ExecutorService exec = Executors.NewFixedThreadPool(fileCount); try { IList <Future <bool> > futures = exec.InvokeAll(tasks); // files should be public foreach (Future <bool> future in futures) { NUnit.Framework.Assert.IsTrue(future.Get()); } // for each path exactly one file status call should be made foreach (AtomicInteger count in counts.Values) { NUnit.Framework.Assert.AreSame(count.Get(), 1); } } finally { exec.Shutdown(); } }
public IndexModel(ILogger <IndexModel> logger, CacheLoader cacheLoader) { _logger = logger; _cacheLoader = cacheLoader; //TODO Fix api calls _cacheLoader.APIToCache(); movies = _cacheLoader.LoadMoviesAsync().Result; }
public void Cache_Single() { var source = GetNumericKeys(1); var loader = new CacheLoader <int, string>(source); var cache = new Cache <int, string>(source.Count, loader); Assert.IsTrue(cache[0] == source[0]); Assert.IsTrue(cache.Misses == 1); }
public void Cache_SingleNull() { var source = GetNumericKeys(1); var loader = new CacheLoader <int, string>(source); var cache = new Cache <int, string>(source.Count, loader); try { Assert.IsNull(cache[1]); } catch (KeyNotFoundException) { // This is the expected result. } Assert.IsTrue(cache.Misses == 1); }
protected void rollLastNameButton_Click(object sender, EventArgs e) { if (HttpRuntime.Cache.Get("names") == null) { CacheLoader.loadRandomTables(); } Dictionary <String, NameTable> names = (Dictionary <String, NameTable>)HttpRuntime.Cache.Get("names"); String name = null; if (raceDropDown.SelectedValue == "Human") { name = names["humanNames"].getLastName(); } else if (raceDropDown.SelectedValue == "Elven") { name = names["elvenNames"].getLastName(); } else if (raceDropDown.SelectedValue == "Dwarven") { name = names["dwarvenNames"].getLastName(); } else if (raceDropDown.SelectedValue == "Halfling") { name = names["halflingNames"].getLastName(); } else if (raceDropDown.SelectedValue == "Gnomish") { name = names["gnomishNames"].getLastName(); } else if (raceDropDown.SelectedValue == "Gnoll") { name = names["gnollNames"].getLastName(); } if (name != null) { lastNameTextBox.Text = name; } }
public void Cache_Performance() { var source = GetRandomStringKeys(1000000); var loader = new CacheLoader <string, string>(source); var cache = new Cache <string, string>(source.Count, loader); var fill = ProcessSource(source, cache); var retrieve = ProcessSource(source, cache); Console.WriteLine( "Cache fill time of '{0}' milliseconds.", fill.TotalMilliseconds); Console.WriteLine( "Cache retrieve time of '{0}' milliseconds.", retrieve.TotalMilliseconds); Console.WriteLine( "Cache performance increase of '{0:#.00}' times.", fill.TotalMilliseconds / retrieve.TotalMilliseconds); Assert.IsTrue( retrieve.TotalMilliseconds < fill.TotalMilliseconds / 3, "It should be at least 3 times quicker to retrieve cached " + "items than add them in this test."); }
static void Main(string[] args) { // We should saturate CPU, Network, and Disk for best perf. // Ideally producer/consumer with back-pressure is required: load data from disk and parse, pass to Streamer. // For simplicity let's just have single-threaded method to load single table, then run in parallel for multiple tables. // Records are fixed length. Only 3 data types are used across all tables: // STRING // INTEGER(8) // DOUBLE // Record format is defined in *.ctl files AND *.ctrl.gen files // Data files are *.dat.gz, and not all of them have ctl, so we should use *.ctrl.gen ConfigureLogger(); var dir = Path.GetFullPath(args?.FirstOrDefault() ?? Path.Combine("..", "..", "data")); // Tests.TestReadFactPostdataMon(dir); // GenerateModels(dir); CacheLoader.LoadFromPath(dir); }
//----------------------------------------------------------------------------// // SHOP GENERATION PRIVATE METHODS //----------------------------------------------------------------------------// //Generates items from the Common Magic Item Tables private void generateCommonShop(int core = 10, int wonderous = 1) { if (HttpRuntime.Cache.Get("items") == null) { CacheLoader.loadRandomTables(); } MagicItemTable magicItemsTable = (MagicItemTable)HttpRuntime.Cache.Get("items"); for (int i = 0; i < core; i++) { MagicItem item = magicItemsTable.getItem("A"); item.Shop = shop; shop.Items.Add(item, Color.White); itemTable.addRow(item, Color.White); } for (int i = 0; i < wonderous; i++) { MagicItem item = magicItemsTable.getItem("F"); item.Shop = shop; shop.Items.Add(item, Color.White); itemTable.addRow(item, Color.White); } }
//Generates items from the Legendary Magic Item Tables private void generateLegendaryShop(int core = 4, int wonderous = 2) { if (HttpRuntime.Cache.Get("items") == null) { CacheLoader.loadRandomTables(); } MagicItemTable magicItemsTable = (MagicItemTable)HttpRuntime.Cache.Get("items"); for (int i = 0; i < core; i++) { MagicItem item = magicItemsTable.getItem("E"); item.Shop = shop; shop.Items.Add(item, Color.White); itemTable.addRow(item, Color.White); } for (int i = 0; i < wonderous; i++) { MagicItem item = magicItemsTable.getItem("I"); item.Shop = shop; shop.Items.Add(item, Color.White); itemTable.addRow(item, Color.White); } generateVeryRareShop(3, 2); }
//Randomly gets a magic item from the appropriate Item Table, and assigns spells to any scrolls public MagicItem getItem(String letter) { MagicItem item; if (letter == "A") { item = magicItemTableA.ElementAt(random.Next(magicItemTableA.Length)); } else if (letter == "B") { item = magicItemTableB.ElementAt(random.Next(magicItemTableB.Length)); } else if (letter == "C") { item = magicItemTableC.ElementAt(random.Next(magicItemTableC.Length)); } else if (letter == "D") { item = magicItemTableD.ElementAt(random.Next(magicItemTableD.Length)); } else if (letter == "E") { item = magicItemTableE.ElementAt(random.Next(magicItemTableE.Length)); } else if (letter == "F") { item = magicItemTableF.ElementAt(random.Next(magicItemTableF.Length)); } else if (letter == "G") { item = magicItemTableG.ElementAt(random.Next(magicItemTableG.Length)); } else if (letter == "H") { item = magicItemTableH.ElementAt(random.Next(magicItemTableH.Length)); } else if (letter == "I") { item = magicItemTableI.ElementAt(random.Next(magicItemTableI.Length)); } else { item = new MagicItem(); item.Name = "ERROR"; item.Rarity = "ERROR"; item.Value = 0; item.MaximumValue = 0; item.MinimumValue = 0; } //Make a copy of the item so any changes dont mess with the table MagicItem itemCopy = new MagicItem(); itemCopy.Name = item.Name; itemCopy.Rarity = item.Rarity; itemCopy.Value = item.Value; itemCopy.MaximumValue = item.MaximumValue; itemCopy.MinimumValue = item.MinimumValue; //If grabbed item is a scroll, determine its level then acquire a random spell of that level and append the spell name to the item if (itemCopy.Name.Contains("scroll")) { int level = 0; //Check for #, which will be the spell lvl foreach (Char c in itemCopy.Name) { if (Int32.TryParse(Char.ToString(c), out int spellLvl)) { level = spellLvl; } } if (HttpRuntime.Cache.Get("spells") == null) { CacheLoader.loadRandomTables(); } SpellTable spellsTable = (SpellTable)HttpRuntime.Cache.Get("spells"); //Grab a random spell of the appropriate level and add to name itemCopy.Name += $" of {spellsTable.getRandomSpell(level)}"; } return(itemCopy); }
public BoundedLocalCache <K, V> NewBoundedLocalCache(Caffeine <K, V> builder, CacheLoader <K, V> loader, bool isAsync) { StringBuilder sb = new StringBuilder(10); // TODO: convert this to an enum with Flags.. if (builder.IsStrongKeys) { sb.Append("S"); } else { sb.Append("W"); } if (builder.IsStrongValues) { sb.Append("S"); } else { sb.Append("I"); } if (builder.RemovalListener != null) { sb.Append("L"); } if (builder.IsRecordingStats) { sb.Append("S"); } if (builder.Evicts) { sb.Append("M"); if (builder.IsWeighted) { sb.Append("W"); } else { sb.Append("S"); } } if (builder.DoesExpireAfterAccess || builder.DoesExpireVariable) { sb.Append("A"); } if (builder.DoesExpireAfterWrite) { sb.Append("W"); } if (builder.DoesRefreshAfterWrite) { sb.Append("R"); } return((BoundedLocalCache <K, V>)Activator.CreateInstance(cacheTypes[sb.ToString()], builder, loader, isAsync)); }
/// <inheritdoc/> public IQuery GetQuery(string name) { return(cache_.Get(name, CacheLoader <IQuery> .From(query => GetQueryTupple(name)))); }
/// <summary> /// Takes a source of cacheable items and tests the cache statistics /// using fixed loading and retrieval patterns. /// </summary> /// <typeparam name="K"></typeparam> /// <typeparam name="V"></typeparam> /// <param name="source"></param> private static void ValidateCache <K, V>(IDictionary <K, V> source) where V : IEquatable <V> { var loader = new CacheLoader <K, V>(source); var cache = new Cache <K, V>(source.Count / 2, loader); // Fill the cache with half of the values. foreach (var item in source.Take(source.Count / 2)) { var expectedLast = cache._linkedList.Last; Assert.IsTrue(cache[item.Key].Equals(item.Value)); Assert.IsTrue(cache._linkedList.First.Value.Key.Equals(item.Key)); Assert.IsTrue(expectedLast == null || expectedLast == cache._linkedList.Last); } Assert.IsTrue(cache.Misses == loader.Fetches); Assert.IsTrue(cache.Misses == source.Count / 2); Assert.IsTrue(cache.Requests == source.Count / 2); // Check all the values are returned from the cache. foreach (var item in source.Take(source.Count / 2)) { var expectedLast = cache._linkedList.Last.Previous; Assert.IsTrue(cache[item.Key].Equals(item.Value)); Assert.IsTrue(cache._linkedList.First.Value.Key.Equals(item.Key)); Assert.IsTrue(expectedLast == cache._linkedList.Last); } Assert.IsTrue(cache.Misses == loader.Fetches); Assert.IsTrue(cache.Misses == source.Count / 2); Assert.IsTrue(cache.Requests == source.Count); // Now use the 2nd half of the source to push out all // the first half. foreach (var item in source.Skip(source.Count / 2)) { var expectedLast = cache._linkedList.Last.Previous; Assert.IsTrue(cache[item.Key].Equals(item.Value)); Assert.IsTrue(cache._linkedList.First.Value.Key.Equals(item.Key)); Assert.IsTrue(expectedLast == cache._linkedList.Last); } Assert.IsTrue(cache.Misses == loader.Fetches); Assert.IsTrue(cache.Misses == source.Count); Assert.IsTrue(cache.Requests == source.Count * 1.5); // Still using the 2nd half of the source retrieve all // the values again. They should come from the cache. foreach (var item in source.Skip(source.Count / 2)) { var expectedLast = cache._linkedList.Last.Previous; Assert.IsTrue(cache[item.Key].Equals(item.Value)); Assert.IsTrue(cache._linkedList.First.Value.Key.Equals(item.Key)); Assert.IsTrue(expectedLast == cache._linkedList.Last); } Assert.IsTrue(cache.Misses == loader.Fetches); Assert.IsTrue(cache.Misses == source.Count); Assert.IsTrue(cache.Requests == source.Count * 2); // Check that the 1st half of the source is now fetched // again and are not already in the cache. foreach (var item in source.Take(source.Count / 2)) { var expectedLast = cache._linkedList.Last.Previous; Assert.IsTrue(cache[item.Key].Equals(item.Value)); Assert.IsTrue(cache._linkedList.First.Value.Key.Equals(item.Key)); Assert.IsTrue(expectedLast == cache._linkedList.Last); } Assert.IsTrue(cache.Misses == loader.Fetches); Assert.IsTrue(cache.Misses == source.Count * 1.5); Assert.IsTrue(cache.Requests == source.Count * 2.5); // Go through in random order and check there are no cache // missed. foreach (var item in source.Take(source.Count / 2).OrderBy(i => Guid.NewGuid())) { var expectedLast = cache._linkedList.Last; Assert.IsTrue(cache[item.Key].Equals(item.Value)); Assert.IsTrue(cache._linkedList.First.Value.Key.Equals(item.Key)); Assert.IsTrue(expectedLast.Value.Key.Equals(item.Key) || expectedLast == cache._linkedList.Last); } Assert.IsTrue(cache.Misses == loader.Fetches); Assert.IsTrue(cache.Misses == source.Count * 1.5); Assert.IsTrue(cache.Requests == source.Count * 3); }
public BoundedLocalCacheStrongKeyStrongValueListener(Caffeine <K, V> builder, CacheLoader <K, V> loader, bool isAsync) : base(builder, loader, isAsync) { this.RemovalListener = builder.RemovalListener; }
protected void rollFullNameButton_Click(object sender, EventArgs e) { if (HttpRuntime.Cache.Get("names") == null) { CacheLoader.loadRandomTables(); } Dictionary <String, NameTable> names = (Dictionary <String, NameTable>)HttpRuntime.Cache.Get("names"); String[] name = null; if (raceDropDown.Text == "Human" && sexDropDown.Text == "Male") { name = names["humanNames"].getMaleFullName(); } else if (raceDropDown.Text == "Human" && sexDropDown.Text == "Female") { name = names["humanNames"].getFemaleFullName(); } else if (raceDropDown.Text == "Elven" && sexDropDown.Text == "Male") { name = names["elvenNames"].getMaleFullName(); } else if (raceDropDown.Text == "Elven" && sexDropDown.Text == "Female") { name = names["elvenNames"].getFemaleFullName(); } else if (raceDropDown.Text == "Dwarven" && sexDropDown.Text == "Male") { name = names["dwarvenNames"].getMaleFullName(); } else if (raceDropDown.Text == "Dwarven" && sexDropDown.Text == "Female") { name = names["dwarvenNames"].getFemaleFullName(); } else if (raceDropDown.Text == "Halfling" && sexDropDown.Text == "Male") { name = names["halflingNames"].getMaleFullName(); } else if (raceDropDown.Text == "Halfling" && sexDropDown.Text == "Female") { name = names["halflingNames"].getFemaleFullName(); } else if (raceDropDown.Text == "Gnomish" && sexDropDown.Text == "Male") { name = names["gnomishNames"].getMaleFullName(); } else if (raceDropDown.Text == "Gnomish" && sexDropDown.Text == "Female") { name = names["gnomishNames"].getFemaleFullName(); } else if (raceDropDown.Text == "Gnoll" && sexDropDown.Text == "Male") { name = names["gnollNames"].getMaleFullName(); } else if (raceDropDown.Text == "Gnoll" && sexDropDown.Text == "Female") { name = names["gnollNames"].getFemaleFullName(); } if (name != null) { firstNameTextBox.Text = name[0]; lastNameTextBox.Text = name[1]; } }
public BoundedLocalCacheStrongKeyStrongValue(Caffeine <K, V> builder, CacheLoader <K, V> loader, bool isAsync) : base(builder, loader, isAsync) { }
/// <summary> /// 新建缓存 /// </summary> /// <param name="minute">缓存时间(分钟)</param> /// <param name="loader">获取数据的方法</param> public LocalCache(int minute, CacheLoader loader) { retentionTime = minute; this.loader = loader; memoryCache = MemoryCache.Default; }
/// <summary> /// 使用主键批量获取对象集合。 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="session"></param> /// <param name="ids"></param> /// <returns></returns> public static IList <T> GetList <T>(this ISession session, IEnumerable ids) { CacheLoader loader = new CacheLoader(); var entityPersister = session.GetSessionImplementation().GetEntityPersister(typeof(T).FullName, null); var entityName = typeof(T).FullName; var eventSource = (IEventSource)session; var loadType = LoadEventListener.Get; List <T> output = new List <T>(); ArrayList missings = new ArrayList(); foreach (object id in ids) { EntityKey entityKey = new EntityKey(id, entityPersister, EntityMode.Poco); LoadEvent loadEvent = new LoadEvent(id, entityName, false, eventSource); var entity = loader.LoadFromSessionCache(loadEvent, entityKey, loadType); if (entity == null) { entity = loader.LoadFromSecondLevelCache(loadEvent, entityPersister, loadType); } if (entity != null) { output.Add((T)entity); } else { missings.Add(id); } } if (missings.Count > 0) { //对组合键进行处理 if (entityPersister.IdentifierType.IsComponentType) { ComponentType idType = (ComponentType)entityPersister.IdentifierType; List <HashSet <object> > props = new List <HashSet <object> >(); foreach (string name in idType.PropertyNames) { props.Add(new HashSet <object>()); } foreach (var missing in missings) { var values = idType.GetPropertyValues(missing, EntityMode.Poco); for (int i = 0; i < values.Length; i++) { props[i].Add(values[i]); } } var cr = session.CreateCriteria(entityName); //合并为查询 int index = 0; foreach (HashSet <object> set in props) { string propName = entityPersister.IdentifierPropertyName + "." + idType.PropertyNames[index]; if (set.Count == 1) { cr = cr.Add(Expression.Eq(propName, set.First())); } else { cr = cr.Add(Expression.In(propName, set.ToArray())); } index++; } output.AddRange(cr.List <T>()); } else { output.AddRange(session.CreateCriteria(entityName) .Add(Expression.In(entityPersister.IdentifierPropertyName, missings)) .List <T>()); } } return(output); }
public override T GetByID(Guid id) { return(cache_.Get(Key(id), CacheLoader <T> .From(key => base.GetByID(id)))); }
public _CacheLoader_328(ConcurrentMap <Path, AtomicInteger> counts, CacheLoader <Path , Future <FileStatus> > loader) { this.counts = counts; this.loader = loader; }