public static void Main() { // Make sure to start an Ignite server node before. Ignition.ClientMode = true; using (var ignite = Ignition.StartFromApplicationConfiguration()) { Console.WriteLine(">>> Client node connected to the cluster"); // Creating a distributed and near cache. var nearCacheCfg = new NearCacheConfiguration { EvictionPolicy = new LruEvictionPolicy { // Near cache will store only 10 recently accessed/used entries. MaxSize = 10 } }; // Enable .NET near cache: keeps data in CLR heap to avoid deserialization costs. var platformNearCacheCfg = new PlatformNearCacheConfiguration(); Console.WriteLine(">>> Populating the cache..."); ICache <int, int> cache = ignite.GetOrCreateCache <int, int>( new CacheConfiguration(CacheName), nearCacheCfg, platformNearCacheCfg); // Adding data into the cache. // Latest 10 entries will be stored in the near cache on the client node side. for (int i = 0; i < 1000; i++) { cache.Put(i, i * 10); } Console.WriteLine(">>> Cache size: [Total={0}, Near={1}]", cache.GetSize(), cache.GetSize(CachePeekMode.PlatformNear)); Console.WriteLine("\n>>> Reading from near cache..."); // Read data directly from .NET cache, without network or JVM calls, and without deserialization. foreach (var entry in cache.GetLocalEntries(CachePeekMode.PlatformNear)) { Console.WriteLine(entry); } Console.WriteLine("\n>>> Example finished, press any key to exit ..."); Console.ReadKey(); } }
/// <summary> /// Gets or creates near cache. /// </summary> private ICache <TK, TV> GetOrCreateNearCache0 <TK, TV>(string name, NearCacheConfiguration configuration, PlatformNearCacheConfiguration platformConfiguration, Op op) { IgniteArgumentCheck.NotNull(configuration, "configuration"); var cacheTarget = DoOutOpObject((int)op, w => { w.WriteString(name); configuration.Write(w); if (platformConfiguration != null) { w.WriteBoolean(true); platformConfiguration.Write(w); } else { w.WriteBoolean(false); } }); return(GetCache <TK, TV>(cacheTarget)); }
/** <inheritdoc /> */ public ICache <TK, TV> GetOrCreateNearCache <TK, TV>(string name, NearCacheConfiguration configuration, PlatformNearCacheConfiguration platformConfiguration) { return(GetOrCreateNearCache0 <TK, TV>(name, configuration, platformConfiguration, Op.GetOrCreateNearCache)); }
/// <summary> /// Gets or creates the cache. /// </summary> private ICache <TK, TV> GetOrCreateCache <TK, TV>(CacheConfiguration configuration, NearCacheConfiguration nearConfiguration, PlatformNearCacheConfiguration platformNearConfiguration, Op op) { IgniteArgumentCheck.NotNull(configuration, "configuration"); IgniteArgumentCheck.NotNull(configuration.Name, "CacheConfiguration.Name"); configuration.Validate(Logger); var cacheTarget = DoOutOpObject((int)op, s => { var w = BinaryUtils.Marshaller.StartMarshal(s); configuration.Write(w, ClientSocket.CurrentProtocolVersion); if (nearConfiguration != null) { w.WriteBoolean(true); nearConfiguration.Write(w); } else { w.WriteBoolean(false); } if (platformNearConfiguration != null) { w.WriteBoolean(true); platformNearConfiguration.Write(w); } else { w.WriteBoolean(false); } }); return(GetCache <TK, TV>(cacheTarget)); }
/** <inheritdoc /> */ public ICache <TK, TV> CreateCache <TK, TV>(CacheConfiguration configuration, NearCacheConfiguration nearConfiguration, PlatformNearCacheConfiguration platformNearConfiguration) { return(GetOrCreateCache <TK, TV>(configuration, nearConfiguration, platformNearConfiguration, Op.CreateCacheFromConfig)); }