public void AddTest()
        {
            GenericCache <int, int> cache = new GenericCache <int, int>();

            cache.Add(1, 1001);
            cache.Add(2, 1002);
            Assert.AreEqual <int>(2, cache.Count);
        }
        public void ContainsKeyTest()
        {
            GenericCache <int, int> cache = new GenericCache <int, int>();

            cache.Add(1, 1001);
            cache.Add(2, 1002);
            Assert.IsTrue(cache.ContainsKey(1));
            Assert.IsFalse(cache.ContainsKey(3));
        }
        public void TryGetValueTest()
        {
            GenericCache <int, int> cache = new GenericCache <int, int>();

            cache.Add(1, 1001);
            cache.Add(2, 1002);
            int result;

            Assert.IsTrue(cache.TryGetValue(1, out result));
            Assert.AreEqual <int>(1001, result);
            Assert.IsFalse(cache.TryGetValue(3, out result));
        }
 public static void Add(Type type, object item)
 {
     if (type is null || item is null)
     {
         throw new NullReferenceException();
     }
     _cache_.Add(type, item);
 }
예제 #5
0
        /// <summary>
        /// Creates and index if it doesn't already exist with options, e.g. ensure unique. Does not compare index options so a change to these must be handled manually.
        /// </summary>
        protected void EnsureIndexExists <TDocument>(IMongoCollection <TDocument> collection, CreateIndexOptions indexOptions, params string[] keyNames)
        {
            ConcurrentDictionary <string, object> indexes;

            List <IndexKeysDefinition <TDocument> > indexFields = new List <IndexKeysDefinition <TDocument> >();

            foreach (string item in  keyNames)
            {
                indexFields.Add(Builders <TDocument> .IndexKeys.Ascending(item));
            }
            IndexKeysDefinition <TDocument> index;

            if (indexFields.Count > 1)
            {
                index = Builders <TDocument> .IndexKeys.Combine(indexFields);
            }
            else
            {
                index = indexFields[0];
            }

            if (!_IndexCache.TryGetItem(collection.CollectionNamespace.FullName, out indexes))
            {
                indexes = new ConcurrentDictionary <string, object>();
                _IndexCache.Add(collection.CollectionNamespace.FullName, indexes);
            }

            StringBuilder indexNameBuilder = new StringBuilder();

            foreach (string item in keyNames)
            {
                if (indexNameBuilder.Length > 0)
                {
                    indexNameBuilder.Append("|");
                }
                indexNameBuilder.Append(item);
            }
            string indexName = indexNameBuilder.ToString();

            if (!indexes.ContainsKey(indexName))
            {
                //if (!collection.Indexes.Exists(keyNames))  //nolonger supported, now only can get list of indexes
                {
                    if (indexOptions == null)
                    {
                        collection.Indexes.CreateOne(index);
                    }
                    else
                    {
                        collection.Indexes.CreateOne(index, indexOptions);
                    }
                }
                indexes.TryAdd(indexName, null);
            }
        }
예제 #6
0
        public PSKIdentity GetPSKIdentity(string identity)
        {
            PSKIdentity result = null;

            if (!_PSKIdentities.TryGetItem(identity, out result))
            {
                IMongoDatabase database = GetDatabase(DATABASE_NAME, true);
                IMongoCollection <BsonDocument> collection = database.GetCollection <BsonDocument>(COLLECTION_NAME);
                BsonDocument doc = collection.Find(Builders <BsonDocument> .Filter.Eq("_id", identity)).FirstOrDefault();
                result = LoadPSKIdentityFromDoc(doc);
                _PSKIdentities.Add(identity, result);
            }
            return(result);
        }
예제 #7
0
        public AccessKey GetAccessKey(string key)
        {
            AccessKey result;

            if (!_AccessKeys.TryGetItem(key, out result))
            {
                IMongoDatabase database = GetDatabase(DATABASE_NAME, false);
                IMongoCollection <BsonDocument> collection = database.GetCollection <BsonDocument>("AccessKey");
                BsonDocument doc = collection.Find(Builders <BsonDocument> .Filter.Eq("_id", key)).FirstOrDefault();
                if (doc != null)
                {
                    result = LoadAccessKey(doc);
                }
                _AccessKeys.Add(key, result);
            }
            return(result);
        }
예제 #8
0
        public void GenericCacheTest()
        {
            var cacheHelper = new GenericCache <string, string>();

            cacheHelper.Add("hello", "world");

            Assert.AreEqual <int>(1, cacheHelper.Count);
            Assert.AreEqual <bool>(true, cacheHelper.ContainsKey("hello"));
            Assert.AreEqual <bool>(false, cacheHelper.ContainsKey("hi"));

            var val = "";

            cacheHelper.TryGetValue("hello", out val);
            Assert.AreEqual <string>("world", val);

            cacheHelper.Clear();
            Assert.AreEqual <int>(0, cacheHelper.Count);
        }
예제 #9
0
        private static void SimpleValidation()
        {
            // Create cache
            GenericCache <long, string> toStringCache = new GenericCache <long, string>(
                CacheFactory.Build <string>(
                    settings => settings
                    .WithProtoBufSerializer()
                    .WithSQLiteCacheHandle(new SQLiteCacheHandleAdditionalConfiguration {
                DatabaseFilePath = "MyDatabase.sqlite"
            })
                    .Build()));

            // Initial state
            toStringCache.Exists(1).Should().BeFalse();

            // Add
            toStringCache.Add(1, "1").Should().BeTrue();
            toStringCache.Exists(1).Should().BeTrue();

            // Get
            toStringCache.Get(1).Should().Be("1");
        }
예제 #10
0
    public void Init()
    {
        // Store prefabs in cache
        _Cache.Add("Agent-Lumberjack", Resources.Load <GameObject>("Agent-Lumberjack"));
        _Cache.Add("Agent-Miner", Resources.Load <GameObject>("Agent-Miner"));
        _Cache.Add("Agent-Farmer", Resources.Load <GameObject>("Agent-Farmer"));
        _Cache.Add("Agent-Soldier", Resources.Load <GameObject>("Agent-Soldier"));
        _Cache.Add("Tree", Resources.Load <GameObject>("Tree"));
        _Cache.Add("Ore", Resources.Load <GameObject>("Ore"));
        _Cache.Add("Farm", Resources.Load <GameObject>("Farm"));
        _Cache.Add("Lumberyard", Resources.Load <GameObject>("Lumberyard"));
        _Cache.Add("Mine", Resources.Load <GameObject>("Mine"));
        _Cache.Add("Headquarters", Resources.Load <GameObject>("Headquarters"));
        _Cache.Add("CapturePoint", Resources.Load <GameObject>("CapturePoint"));

        // Get all CapturePoints in scene
        _capturePointsInScene = MonoBehaviour.FindObjectsOfType <CapturePoint>();
        _CapturePointsList    = new List <GameObject>();

        // Transfer colliders into gameObject list
        foreach (CapturePoint cp in _capturePointsInScene)
        {
            _CapturePointsList.Add(cp.gameObject);
        }
    }
예제 #11
0
 public void Add(string key, DataTable dt)
 {
     genericCache.Add(key, dt);
 }