public void MemcachedCacheFactory()
        {
            var factory =
                new MemcachedFactory(new Dictionary <string, string>
            {
                {
                    "configuration", @"{
  ""Servers"": [
    {
      ""Address"": ""memcached"",
      ""Port"": 11211
    }
  ],
  ""Authentication"": {
    ""Type"": ""Enyim.Caching.Memcached.PlainTextAuthenticator"",
    ""Parameters"": {
      ""zone"": """",
      ""userName"": ""username"",
      ""password"": ""password""
    }
  }
}"
                }
            });
            var cache1 = factory.BuildCache();

            Assert.That(cache1, Is.Not.Null, "Factory has yielded null");
            Assert.That(cache1, Is.InstanceOf <MemcachedClient>(), "Unexpected cache");
            var cache2 = factory.BuildCache();

            Assert.That(cache2, Is.EqualTo(cache1),
                        "The Memcached cache factory is supposed to always yield the same instance");

            var keySanitizer = factory.Constraints?.KeySanitizer;

            Assert.That(keySanitizer, Is.Not.Null, "Factory lacks a key sanitizer");
            Assert.That(keySanitizer("--abc \n\r\t\v\fdef--"), Is.EqualTo("--abc------def--"), "Unexpected key sanitization");
        }
示例#2
0
        public void Test()
        {
            var mcf = A.Fake<IMemcachedClientFactory>();
            var mcc = A.Fake<IMemcachedClient>();
            A.CallTo(() => mcf.Create(A<IMemcachedSettings>._)).Returns(mcc);
            A.CallTo(() => mcc.Get<byte[]>(A<string>._)).Returns(null);
            A.CallTo(() => mcc.Store(StoreMode.Add, A<string>._, A<object>._, A<TimeSpan>._)).Returns(true);

            IMemcachedFactory mf = new MemcachedFactory(mcf);
            var chmc = mf.Create(
                MemcachedSettings.Settings
                    .Prefix("test")
                    .Server("localhost", 12345)
                    .TtlMin(TimeSpan.FromSeconds(30))
                    .TtlRange(TimeSpan.FromSeconds(3000))
                    .LockTime(TimeSpan.FromSeconds(15))
                );

            string value;
            var result = chmc.TryGetOrAdd("test", () => "value", out value);

            A.CallTo(() => mcc.Store(StoreMode.Set, A<string>._, A<object>._, A<TimeSpan>._)).MustHaveHappened();
            A.CallTo(() => mcc.Remove(A<string>._)).MustHaveHappened();
        }