public void DontCacheExceptions() { var i = 0; var cache = new LazyCache <string>((k) => { i++; if (i == 1) { throw new Exception(); } return("test"); }, 1); try { var first = cache.Get("key"); } catch (Exception) { } var second = cache.Get("key"); Assert.IsTrue(second == "test"); }
public CN() { RegionsCache = LazyCache.Create(() => { var fileStorage = Application.Ioc.Resolve <IFileStorage>(); var json = fileStorage.GetResourceFile("texts", "regions_cn.json").ReadAllText(); return(JsonConvert.DeserializeObject <List <Regions.Region> >(json)); }); }
public void OnlyOneInParallel_LongGetOperation() { var i = 0; var cache = new LazyCache <string>((k) => { i++; Thread.Sleep(1000); return("test"); }, 1); Parallel.ForEach(Enumerable.Range(1, 3), (a) => cache.Get("key")); Assert.IsTrue(i == 1); }
public void OnlyOneInParallel() { var i = 0; var cache = new LazyCache <string>((k) => { i++; return("test"); }, 1); Parallel.ForEach(Enumerable.Range(1, 5), (a) => cache.Get("key")); Assert.IsTrue(i == 1); }
public void Init() { Cache = new LazyCache <string>((key) => { return(_storage.ContainsKey(key) ? Task.FromResult(_storage[key]) : Task.FromResult <string>(null)); }); }
public JP() { RegionsCache = LazyCache.Create(() => { var pathManager = Application.Ioc.Resolve <PathManager>(); var path = pathManager.GetResourceFullPath("texts", "regions_jp.json"); var json = File.ReadAllText(path); return(JsonConvert.DeserializeObject <List <Model.Region> >(json)); }); }
public void Test() { var instance = LazyCache <ITestType> .Create(() => new TestType()); var message1 = instance.Message; var message2 = instance.Message; Assert.StartsWith("Test ", message1); Assert.Equal(message1, message2); }
protected override unsafe void ReadFields(byte *d) { base.ReadFields(d); m_iHealth = *(int *)(d + DT_BasePlayer.Instance.m_iHealth); m_lifeState = *(LifeState *)(d + DT_BasePlayer.Instance.m_lifeState); m_iDefaultFOV = *(int *)(d + DT_BasePlayer.Instance.m_iDefaultFOV); m_hObserverTarget = *(int *)(d + DT_BasePlayer.Instance.m_hObserverTarget); m_fFlags = *(Flags *)(d + DT_BasePlayer.Instance.m_fFlags); m_iObserverMode = *(ObserverMode *)(d + DT_BasePlayer.Instance.m_iObserverMode); m_hActiveWeapon = *(int *)(d + DT_BaseCombatCharacter.Instance.m_hActiveWeapon) & 0xFFF; m_ActiveWeapon = new LazyCache <BaseCombatWeapon>(GetWeapon); }
public StateModule() : base(Program.Hack, ModulePriority.Highest) { LocalPlayer = new LazyCache <CSLocalPlayer>(() => { var address = Program.Hack.Memory.Read <IntPtr>(pLocalPlayer); if ((long)address == 0) { return(null); } var lp = new CSLocalPlayer(address); return(lp); }); ClientState = new LazyCache <ClientState>(() => { var address = Program.Hack.Memory.Read <int>(pClientState); if (address == 0 || address == -1) { return(null); } return(new ClientState(address)); }); GameDirectory = new LazyCache <string>(() => { return(Program.Hack.Memory.ReadString(Program.Hack.EngineDll.BaseAddress.ToInt32() + Program.Offsets.GameDirectory, 256, Encoding.UTF8)); }); //GameRules = new LazyCache<CSGameRulesProxy>(() => //{ // var address = Program.Hack.Memory.Read<int>(pGameRules); // if (address == 0 || address == -1) // return null; // //var grp = Program.Hack.GetEntityByAddress<CSGameRulesProxy>(address); // var grp = Program.Hack.GetEntityByAddress<CSGameRulesProxy>(pGameRules); // return grp; //}); PlayerResources = new LazyCache <CSPlayerResource>(() => { var address = Program.Hack.Memory.Read <IntPtr>(pPlayerResources); if ((int)address == 0) { return(null); } return(Program.Hack.GetEntityByAddress <CSPlayerResource>(address)); }); ViewMatrix = new LazyCache <Matrix>(() => Program.Hack.Memory.Read <Matrix>(pViewMatrix)); }
/// <summary> /// 初始化 /// </summary> public Country() { RegionsCache = LazyCache.Create(() => new List <Regions.Region>()); RegionsTreeCache = LazyCache.Create(() => { var regions = RegionsCache.Value; var regionsMapping = regions.ToDictionary(r => r.Id); return(TreeUtils.CreateTree(regions, r => r, r => regionsMapping.GetOrDefault(r.ParentId))); }); RegionsTreeNodeCache = LazyCache.Create(() => { var tree = GetRegionsTree(); return(tree.EnumerateAllNodes() .Where(n => n.Value != null).ToDictionary(n => n.Value.Id)); }); }
public Grid( LazyCache <Vector2Int> size = null, GridTotals totals = null, ObservableCollection <T> gridMembers = null, Dictionary <Vector2Int, T> occupiedTiles = null, Dictionary <T, List <T> > neighbourDict = null) { Size = size ?? new LazyCache <Vector2Int>(() => { var bounds = new GridBounds(OccupiedTiles.Keys); return(bounds.GetDelta()); }); Totals = totals ?? new GridTotals(); GridMembers = gridMembers ?? new ObservableCollection <T>(); OccupiedTiles = occupiedTiles ?? new Dictionary <Vector2Int, T>(); NeighbourDict = neighbourDict ?? new Dictionary <T, List <T> >(); }
public ManagedClientClass(IntPtr address) { Address = address; ClientClass = new LazyCache <ClientClass>(() => Program.Hack.Memory.Read <ClientClass>(address)); var c = ClientClass; NetworkName = new LazyCache <string>(() => Program.Hack.Memory.ReadString(c.Value.m_pNetworkName, 32, Encoding.ASCII)); if (c.Value.m_pRecvTable == 0xffff || c.Value.m_pRecvTable == -1 || c.Value.m_pRecvTable == 0) { RecvTable = null; } else { RecvTable = new ManagedRecvTable(c.Value.m_pRecvTable); } }
public ClientState(int address) { this.address = address; data = new byte[MAX_SIZE]; Program.Hack.Memory.Position = address; Program.Hack.Memory.Read(data, 0, data.Length); stream = new MemoryStream(data); ViewAngles = new LazyCache <Vector3>(() => ReadAt <Vector3>(Program.Offsets.ClientStateSetViewAngles)); Map = new LazyCache <string>(() => { var str = Encoding.UTF8.GetString(data, Program.Offsets.ClientStateMapDirectory, 256); if (str.Contains("\0")) { str = str.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries)[0]; } return(str); }); State = new LazyCache <SignOnState>(() => ReadAt <SignOnState>(Program.Offsets.ClientStateState)); }
public void All() { var createCount = 0; var cache = LazyCache.Create(() => { createCount += 1; return(new object()); }); Assert.IsTrue(!cache.IsValueCreated); var a = cache.Value; var b = cache.Value; Assert.IsTrue(cache.IsValueCreated); Assert.Equals(createCount, 1); Assert.IsTrue(object.ReferenceEquals(a, b)); cache.Reset(); Assert.IsTrue(!cache.IsValueCreated); a = cache.Value; b = cache.Value; Assert.IsTrue(cache.IsValueCreated); Assert.Equals(createCount, 2); Assert.IsTrue(object.ReferenceEquals(a, b)); }
public ManagedRecvTable(int address) { Address = address; RecvTable = new LazyCache <RecvTable_t>(() => Program.Hack.Memory.Read <RecvTable_t>(address)); var r = RecvTable; NetTableName = new LazyCache <string>(() => Program.Hack.Memory.ReadString(r.Value.m_pNetTableName, 32, Encoding.ASCII)); RecvProps = new LazyCache <ManagedRecvProp[]>(() => { ManagedRecvProp[] props = new ManagedRecvProp[r.Value.m_nProps]; for (int i = 0; i < props.Length; i++) { props[i] = new ManagedRecvProp(r.Value.m_pProps + i * 0x3C, this); } return(props); }); HighestOffset = new LazyCache <int>(() => { return(RecvProps.Value.Length == 0 ? 0 : RecvProps.Value.Max(x => x == null ? 0 : x.Offset));// GetHighestOffset(this); }); BaseClassDepth = new LazyCache <int>(() => { if (BaseClass.Value != null) { return(1 + BaseClass.Value.BaseClassDepth); } return(0); }); BaseClass = new LazyCache <ManagedRecvTable>(() => { if (RecvProps.Value.Any(x => x.VarName == "baseclass")) { return(RecvProps.Value.First(x => x.VarName == "baseclass").SubTable); } return(null); }); Size = new LazyCache <int>(() => RecvProps.Value.Length > 0 ? RecvProps.Value.Last().Size : 0); }
public void CacheIsAutoRefreshing() { var i = 0; var cache = new LazyCache <string>((k) => { i++; if (i == 2) { Thread.Sleep(200); } return("test"); }, 1.0 / 60); var first = cache.Get("key"); Thread.Sleep(1500); var duration = Duration(() => { var second = cache.Get("key"); }); Assert.IsTrue(duration.TotalMilliseconds < 200); }
public Node(int value) { cache = new LazyCache <int>(() => this.node1.Value.Value + this.node2.Value.Value); cache.Value = value; }
protected BaseListener(string path) { this.path = path; cache = new LazyCache <T>(GetContent); }
public Node(Node node1, Node node2) { this.node1 = new Cell <Node>(node1); this.node2 = new Cell <Node>(node2); cache = new LazyCache <int>(() => this.node1.Value.Value + this.node2.Value.Value); }
public void TestCascadingLazyCache() { // We set one cell and two caches in cascade var cell = new Cell <int>(2); var cache1 = new LazyCache <int>(() => cell.Value + 1); var cache2 = new LazyCache <int>(() => cache1.Value * 2); Assert.IsFalse(cache1.IsCached); Assert.AreEqual(0, cache1.BeaconCount); Assert.IsFalse(cache2.IsCached); Assert.AreEqual(0, cache2.BeaconCount); // Getting the value of cache number 2 var value = cache2.Value; Assert.IsTrue(cache1.IsCached); Assert.AreEqual(1, cache1.BeaconCount); Assert.IsTrue(cache2.IsCached); Assert.AreEqual(1, cache2.BeaconCount); // Checking that setting the base cell invalidate all caches cell.Value = 3; Assert.IsFalse(cache1.IsCached); Assert.AreEqual(0, cache1.BeaconCount); Assert.IsFalse(cache2.IsCached); Assert.AreEqual(0, cache2.BeaconCount); // Querying the value of cache1 value = cache1.Value; Assert.IsTrue(cache1.IsCached); Assert.AreEqual(1, cache1.BeaconCount); Assert.AreEqual(4, value); Assert.IsFalse(cache2.IsCached); Assert.AreEqual(0, cache2.BeaconCount); // Get the value of cache2 now value = cache2.Value; Assert.IsTrue(cache1.IsCached); Assert.AreEqual(1, cache1.BeaconCount); Assert.IsTrue(cache2.IsCached); Assert.AreEqual(1, cache2.BeaconCount); Assert.AreEqual(8, value); // Set cache1 to an arbitrary value. This should invalidate the cache2 cache1.Value = 34; Assert.IsTrue(cache1.IsCached); Assert.IsFalse(cache2.IsCached); Assert.AreEqual(0, cache1.BeaconCount); Assert.AreEqual(0, cache2.BeaconCount); // Get the value of cache2 value = cache2.Value; Assert.IsTrue(cache1.IsCached); Assert.IsTrue(cache2.IsCached); Assert.AreEqual(0, cache1.BeaconCount); Assert.AreEqual(1, cache2.BeaconCount); Assert.AreEqual(68, value); // Setting the cell to a different value should have no impact cell.Value++; Assert.IsTrue(cache1.IsCached); Assert.IsTrue(cache2.IsCached); Assert.AreEqual(0, cache1.BeaconCount); Assert.AreEqual(1, cache2.BeaconCount); Assert.AreEqual(68, cache2.Value); // Clearing the cache1 should invalidate cache2 cache1.Clear(); Assert.IsFalse(cache1.IsCached); Assert.AreEqual(0, cache1.BeaconCount); Assert.IsFalse(cache2.IsCached); Assert.AreEqual(0, cache2.BeaconCount); // The value of cache2 should be different now value = cache2.Value; Assert.IsTrue(cache1.IsCached); Assert.IsTrue(cache2.IsCached); Assert.AreEqual(1, cache1.BeaconCount); Assert.AreEqual(1, cache2.BeaconCount); Assert.AreEqual(10, value); }
public void TestLazyCacheAndSnapshot() { // Creating the cell and monitor var beacon = new Beacon(); var snapshot = false; var cell = new Cell <int>(18); var cache = new LazyCache <int>(() => 11); var count = 0; int last = -1; var monitor = new Monitor(() => { beacon.Register(); var value1 = cell.Value; if (snapshot) { using (new Snapshot()) { last = cache.Value + value1 + 1; } } else { last = cache.Value - value1 + 1; } count++; }); // We're initially in the case where we don't use snapshots Assert.AreEqual(1, count); Assert.AreEqual(-6, last); Assert.AreEqual(1, cell.MonitorCount); Assert.AreEqual(1, cache.MonitorCount); // Monitor is bound to cell1, cache and the beacon Assert.AreEqual(3, monitor.BeaconCount); // If we change the cache, we get the monitor to rerun cache.Value = 23; Assert.AreEqual(2, count); Assert.AreEqual(6, last); // Same thing for the cell cell.Value = 7; Assert.AreEqual(3, count); Assert.AreEqual(17, last); // Now we turn the snapshotting on and signal it snapshot = true; beacon.Signal(); Assert.AreEqual(4, count); Assert.AreEqual(31, last); Assert.AreEqual(1, cell.MonitorCount); Assert.AreEqual(0, cache.MonitorCount); Assert.AreEqual(2, monitor.BeaconCount); // If we change cell, we still have the rerun cell.Value++; Assert.AreEqual(5, count); Assert.AreEqual(32, last); // But if we change cache, nothing happens cache.Value++; Assert.AreEqual(5, count); Assert.AreEqual(32, last); // We need to change cell or the lone beacon to get an update beacon.Signal(); Assert.AreEqual(6, count); Assert.AreEqual(33, last); }
internal Bridge(TOwner owner, string name) { this.owner = owner; externalInfo = Utils.SearchPublicProperty(typeof(TOwner), name); externalCache = new LazyCache <TValue>(() => (TValue)externalInfo.GetValue(owner)); }
public ParentDiff() { this.RemoveList = new HashSet <Guid>(); this.Modify = new List <ModifyEntry>(); this.modifyById = new LazyCache <Dictionary <Guid, ModifyEntry> >(() => this.Modify.ToDictionary(x => x.IdEntry)); }
/// <summary> /// 初始化 /// </summary> public CurrencyManager() { CurrencyCache = LazyCache.Create(() => { return(Application.Ioc.ResolveMany <ICurrency>().ToDictionary(c => c.Type)); }); }
/// <summary> /// Initialize a new empty instance of this class /// </summary> public ParentDiff() { this.RemoveList = new HashSet<Guid>(); this.Modify = new List<ModifyEntry>(); this.modifyById = new LazyCache<Dictionary<Guid, ModifyEntry>>(() => this.Modify.ToDictionary(x => x.IdEntry)); }
public void TestBasicLazyCache() { // Setting a cell and a cache depending on that cell var cell = new Cell <int>(2); var count = 0; var cache = new LazyCache <int>(() => { count++; return(cell.Value * 2); }); Assert.IsFalse(cache.IsCached); Assert.IsFalse(cache.IsSet); Assert.AreEqual(0, count); Assert.AreEqual(0, cache.BeaconCount); // Reading the value for the first time loads the cache var value = cache.Value; Assert.AreEqual(4, value); Assert.IsTrue(cache.IsCached); Assert.IsFalse(cache.IsSet); Assert.AreEqual(1, count); Assert.AreEqual(1, cache.BeaconCount); // Reading the value again doesn't trigger a recalculation value = cache.Value; Assert.AreEqual(4, value); Assert.IsTrue(cache.IsCached); Assert.IsFalse(cache.IsSet); Assert.AreEqual(1, count); Assert.AreEqual(1, cache.BeaconCount); // If we change the cell, the cache reverts back to the initial state cell.Value = 3; Assert.IsFalse(cache.IsCached); Assert.IsFalse(cache.IsSet); Assert.AreEqual(1, count); Assert.AreEqual(0, cache.BeaconCount); // Reading the value again causes the callback to be rerun value = cache.Value; Assert.IsTrue(cache.IsCached); Assert.AreEqual(2, count); Assert.AreEqual(6, value); Assert.AreEqual(1, cache.BeaconCount); // Setting the cache cuts all dependencies cache.Value = 28; Assert.IsTrue(cache.IsCached); Assert.IsTrue(cache.IsSet); Assert.AreEqual(28, cache.Value); Assert.AreEqual(0, cache.BeaconCount); // This time, no dependencies on cell cell.Value++; Assert.AreEqual(28, cache.Value); // If we clear the cache, we revert back to the original state cache.Clear(); Assert.IsFalse(cache.IsCached); Assert.IsFalse(cache.IsSet); Assert.AreEqual(0, cache.BeaconCount); // Now we re-read the value again value = cache.Value; Assert.IsTrue(cache.IsCached); Assert.AreEqual(3, count); Assert.AreEqual(8, value); Assert.AreEqual(1, cache.BeaconCount); }
public ManagedRecvProp(int address, ManagedRecvTable table) { Address = address; Table = table; RecvProp = new LazyCache <RecvProp_t>(() => Program.Hack.Memory.Read <RecvProp_t>(address)); var r = RecvProp; VarName = new LazyCache <string>(() => Program.Hack.Memory.ReadString(r.Value.m_pVarName, 32, Encoding.ASCII)); SubTable = new LazyCache <ManagedRecvTable>(() => { if (r.Value.m_pDataTable == 0) { return(null); } return(new ManagedRecvTable(r.Value.m_pDataTable)); }); BaseClassDepth = new LazyCache <int>(() => SubTable.Value != null ? 1 + SubTable.Value.BaseClassDepth : 0); Size = new LazyCache <int>(() => { if (SubTable.Value != null) { return(SubTable.Value.Size); } int size = RecvProp_t.GetPropTypeSize(Type); if (size >= 0) { return(size); } switch (Type) { case RecvProp_t.ePropType.String: return(StringBufferSize); case RecvProp_t.ePropType.Array: if (ArrayProp.Value.Length > 0) { return(ElementCount * ArrayProp.Value[0].Size); } return(0); } return(0); }); ArrayProp = new LazyCache <ManagedRecvProp[]>(() => { if (this.Type != RecvProp_t.ePropType.Array || this.RecvProp.Value.m_pArrayProp == 0 || this.ElementCount == 0) { return(new ManagedRecvProp[0]); } var props = new ManagedRecvProp[this.ElementCount]; for (int i = 0; i < props.Length; i++) { int a = this.RecvProp.Value.m_pArrayProp + SizeCache <RecvProp_t> .Size * i; if (address == a) { props[i] = this; } else { props[i] = new ManagedRecvProp(a, table); } } return(props); }); }
public MustacheTemplateService(IContentReaderService <TextContentModel> partialsLocator = null) { _partialsLocator = partialsLocator; _cache = new LazyCache <Template>(loadTemplate); }