public void TimeSpan_MaxValue_Should_Never_Expire()
        {
            var e = Expiration.From(TimeSpan.MaxValue);

            Assert.Equal(0u, e.Value);
            Assert.True(e.IsAbsolute);
            Assert.True(e.IsNever);
        }
        public void TimeSpan_Less_Than_One_Month_Should_Become_Valid_Relative_Expiration()
        {
            var e = Expiration.From(TimeSpan.FromSeconds(100));

            Assert.Equal(100u, e.Value);
            Assert.False(e.IsAbsolute);
            Assert.False(e.IsNever);
        }
        public void DateTime_Value_Should_Become_Valid_Absolute_Expiration()
        {
            var e = Expiration.From(new DateTime(2012, 01, 31, 23, 0, 0, DateTimeKind.Utc));

            Assert.Equal(1328050800u, e.Value);
            Assert.True(e.IsAbsolute);
            Assert.False(e.IsNever);
        }
        public void DateTime_MinValue_Should_Never_Expire()
        {
            var e = Expiration.From(DateTime.MaxValue);

            Assert.Equal(0u, e.Value);
            Assert.True(e.IsAbsolute);
            Assert.True(e.IsNever);
        }
        public void Different_Instances_From_Same_DateTime_Must_Be_Equal()
        {
            var a = Expiration.From(new DateTime(2012, 01, 31, 23, 0, 0, DateTimeKind.Utc));
            var b = (Expiration)(new DateTime(2012, 01, 31, 23, 0, 0, DateTimeKind.Utc));

            Assert.True(a.Equals(a), "a = a");
            Assert.True(b.Equals(b), "b = b");

            Assert.True(a.Equals(b), "a = b");
            Assert.True(b.Equals(a), "b = a");
        }
        public void Different_Relative_Instances_From_Same_TimeSpan_Must_Be_Equal()
        {
            var a = Expiration.From(TimeSpan.FromSeconds(100));
            var b = (Expiration)(TimeSpan.FromSeconds(100));

            Assert.True(a.Equals(a), "a = a");
            Assert.True(b.Equals(b), "b = b");

            Assert.True(a.Equals(b), "a = b");
            Assert.True(b.Equals(a), "b = a");
        }
Exemple #7
0
        private Operations.TouchOperation PerformTouch(string key, ulong cas, Expiration expiration)
        {
            var retval = new Operations.TouchOperation(allocator, key, keyFormatter)
            {
                Cas        = cas,
                Expiration = expiration
            };

            retval.Initialize();

            return(retval);
        }
        private async Task <bool> PerformFlushAll(Expiration when)
        {
            try
            {
                var tasks = cluster.Broadcast((_, state) => new Operations.FlushOperation(state.allocator)
                {
                    When = state.when
                }, (allocator, when));
                await Task.WhenAll(tasks).ConfigureAwait(false);

                return(true);
            }
            catch (IOException) { return(false); }
        }
Exemple #9
0
        public async Task <OperationResult> TouchWithResultAsync(string key, Expiration expiration = default)
        {
            try
            {
                var op = PerformTouch(key, cas: 0, expiration);

                await cluster.Execute(op).ConfigureAwait(false);

                return(new OperationResult((OperationStatus)op.StatusCode, op.Cas));
            }
            catch (IOException e)
            {
                return(new OperationResult(e));
            }
        }
Exemple #10
0
        public async Task <ulong> MutateAsync(MutationMode mode, string key, Expiration expiration, ulong delta, ulong defaultValue)
        {
            try
            {
                var result = await PerformMutate(mode, key, expiration, delta, defaultValue, 0).ConfigureAwait(false);

                return(result.Value);
            }
            catch (Exception e)
            {
                LogTo.Error(e);

                return(0);
            }
        }
Exemple #11
0
        public async Task <bool> TouchAsync(string key, Expiration expiration = default)
        {
            try
            {
                var op = PerformTouch(key, cas: 0, expiration);

                await cluster.Execute(op).ConfigureAwait(false);

                return(op.StatusCode == Protocol.Status.Success);
            }
            catch (IOException)
            {
                return(false);
            }
        }
Exemple #12
0
        private Operations.MutateOperation PerformMutate(
            MutationMode mode, string key,
            ulong delta, ulong defaultValue,
            ulong cas, Expiration expiration, bool silent)
        {
            var retval = new Operations.MutateOperation(allocator, key, keyFormatter, mode, delta, defaultValue)
            {
                Cas        = cas,
                Expiration = expiration,
                Silent     = silent
            };

            retval.Initialize();

            return(retval);
        }
        private Operations.StoreOperation PerformStore(
            StoreMode mode, string key,
            uint flags, SequenceBuilder value,
            ulong cas, Expiration expiration, bool silent)
        {
            var retval = new Operations.StoreOperation(allocator, key, keyFormatter, mode, flags, value)
            {
                Expiration = expiration,
                Cas        = cas,

                Silent = silent
            };

            retval.Initialize();

            return(retval);
        }
        public void Negative_TimeSpan_Should_Become_Valid_Absolute_Expiration()
        {
            var old = SystemTime.Now;

            SystemTime.Set(() => new DateTime(2011, 12, 31, 23, 0, 0, DateTimeKind.Utc));

            try
            {
                var e = Expiration.From(TimeSpan.FromSeconds(-10));

                Assert.Equal(1325372390u, e.Value);
                Assert.True(e.IsAbsolute);
                Assert.False(e.IsNever);
            }
            finally
            {
                SystemTime.Set(old);
            }
        }
        public void TimeSpan_Greater_Than_One_Month_Should_Become_Valid_Absolute_Expiration()
        {
            var old = SystemTime.Now;

            SystemTime.Set(() => new DateTime(2011, 12, 31, 23, 0, 0, DateTimeKind.Utc));

            try
            {
                var e = Expiration.From(TimeSpan.FromDays(31));

                Assert.Equal(1328050800u, e.Value);
                Assert.True(e.IsAbsolute);
                Assert.False(e.IsNever);
            }
            finally
            {
                SystemTime.Set(old);
            }
        }
        public void Different_Absolute_Instances_From_Same_TimeSpan_Must_Be_Equal()
        {
            var old = SystemTime.Now;

            SystemTime.Set(() => new DateTime(2011, 12, 31, 23, 0, 0, DateTimeKind.Utc));

            try
            {
                var a = Expiration.From(TimeSpan.FromDays(60));
                var b = (Expiration)(TimeSpan.FromDays(60));

                Assert.True(a.Equals(a), "a = a");
                Assert.True(b.Equals(b), "b = b");

                Assert.True(a.Equals(b), "a = b");
                Assert.True(b.Equals(a), "b = a");
            }
            finally
            {
                SystemTime.Set(old);
            }
        }
 public static ulong Increment(this ISimpleMemcachedClient self, string key, Expiration expiration, ulong delta = Protocol.MUTATE_DEFAULT_DELTA, ulong defaultValue = Protocol.MUTATE_DEFAULT_VALUE)
 {
     return(self.Mutate(MutationMode.Increment, key, expiration, delta, defaultValue));
 }
Exemple #18
0
		public static Task<bool> AddAsync(this ISimpleMemcachedClient self, string key, object value, Expiration expiration)
		{
			return self.StoreAsync(StoreMode.Add, key, value, expiration);
		}
Exemple #19
0
        public async Task <bool> StoreAsync(StoreMode mode, string key, object value, Expiration expiration = default)
        {
            try
            {
                using var sb = new SequenceBuilder(allocator);

                var flags = itemFormatter.Serialize(sb, value);
                var op    = PerformStore(mode, key, flags, sb, 0, expiration, silent: true);

                await cluster.Execute(op).ConfigureAwait(false);

                return(op.StatusCode == Protocol.Status.Success);
            }
            catch (IOException)
            {
                return(false);
            }
        }
Exemple #20
0
		public static bool Store(this ISimpleMemcachedClient self, StoreMode mode, string key, object value, Expiration expiration)
		{
			return self.StoreAsync(mode, key, value, expiration).RunAndUnwrap();
		}
Exemple #21
0
 public Task <IOperationResult> StoreAsync(StoreMode mode, string key, object value, Expiration expiration, ulong cas)
 {
     return(PerformStoreAsync(mode, key, value, expiration, cas));
 }
Exemple #22
0
        public async Task <IGetOperationResult <T> > GetAndTouchAsync <T>(string key, Expiration expiration, ulong cas)
        {
            var result = await PerformGetAndTouchCore(key, expiration, cas).ConfigureAwait(false);

            var converted = ConvertToResult <T>(result);

            return(converted);
        }
Exemple #23
0
 public static bool Replace(this ISimpleMemcachedClient self, string key, object value, Expiration expiration)
 {
     return(self.Store(StoreMode.Replace, key, value, expiration));
 }
Exemple #24
0
		public static Task<IOperationResult> StoreAsync(this IMemcachedClient self, StoreMode mode, string key, object value, Expiration expiration)
		{
			return self.StoreAsync(mode, key, value, expiration, Protocol.NO_CAS);
		}
Exemple #25
0
        protected async Task <IOperationResult> PerformStoreAsync(StoreMode mode, string key, object value, Expiration expiration, ulong cas)
        {
            try
            {
                using (var ci = transcoder.Serialize(value))
                    using (var kb = keyTransformer.Transform(key))
                    {
                        var op = opFactory.Store(mode, kb, ci, expiration.Value, cas);
                        await cluster.Execute(op).ConfigureAwait(false);

                        return(op.Result);
                    }
            }
            catch (IOException e)
            {
                return(new BinaryOperationResult().FailWith(e));
            }
        }
Exemple #26
0
        protected async Task <IMutateOperationResult> PerformMutate(MutationMode mode, string key, Expiration expiration, ulong delta, ulong defaultValue, ulong cas)
        {
            try
            {
                using (var kb = keyTransformer.Transform(key))
                {
                    var op = opFactory.Mutate(mode, kb, expiration.Value, delta, defaultValue, cas);
                    await cluster.Execute(op).ConfigureAwait(false);

                    return(op.Result);
                }
            }
            catch (IOException e)
            {
                return(new MutateOperationResult().FailWith(e));
            }
        }
		public static IOperationResult Replace(this IMemcachedClient self, string key, object value, Expiration expiration, ulong cas = Protocol.NO_CAS)
		{
			return self.Store(StoreMode.Replace, key, value, expiration, cas);
		}
Exemple #28
0
		public static Task<IOperationResult> AddAsync(this IMemcachedClient self, string key, object value, Expiration expiration, ulong cas = Protocol.NO_CAS)
		{
			return self.StoreAsync(StoreMode.Add, key, value, expiration, cas);
		}
Exemple #29
0
		public static bool Add(this ISimpleMemcachedClient self, string key, object value, Expiration expiration)
		{
			return self.Store(StoreMode.Add, key, value, expiration);
		}
		public static ulong Mutate(this ISimpleMemcachedClient self, MutationMode mode, string key, Expiration expiration, ulong delta = Protocol.MUTATE_DEFAULT_DELTA, ulong defaultValue = Protocol.MUTATE_DEFAULT_VALUE)
		{
			return self.MutateAsync(mode, key, expiration, delta, defaultValue).RunAndUnwrap();
		}
Exemple #31
0
		public static bool Touch(this ISimpleMemcachedClient self, string key, Expiration expiration)
		{
			return self.TouchAsync(key, expiration).RunAndUnwrap();
		}
Exemple #32
0
 public static IMutateOperationResult Decrement(this IMemcachedClient self, string key, Expiration expiration, ulong delta = Protocol.MUTATE_DEFAULT_DELTA, ulong defaultValue = Protocol.MUTATE_DEFAULT_VALUE, ulong cas = Protocol.NO_CAS)
 {
     return(self.Mutate(MutationMode.Decrement, key, expiration, delta, defaultValue, cas));
 }
Exemple #33
0
		public static IOperationResult Touch(this IMemcachedClient self, string key, Expiration expiration, ulong cas = Protocol.NO_CAS)
		{
			return self.TouchAsync(key, expiration, cas).RunAndUnwrap();
		}
Exemple #34
0
        protected virtual async Task <IGetOperationResult> PerformGetAndTouchCore(string key, Expiration expiration, ulong cas)
        {
            try
            {
                using (var kb = keyTransformer.Transform(key))
                {
                    var op = opFactory.GetAndTouch(kb, expiration.Value, cas);
                    await cluster.Execute(op).ConfigureAwait(false);

                    return(op.Result);
                }
            }
            catch (IOException e)
            {
                return(new GetOperationResult().FailWith(e));
            }
        }
		public static IMutateOperationResult Increment(this IMemcachedClient self, string key, Expiration expiration, ulong delta = Protocol.MUTATE_DEFAULT_DELTA, ulong defaultValue = Protocol.MUTATE_DEFAULT_VALUE, ulong cas = Protocol.NO_CAS)
		{
			return self.Mutate(MutationMode.Increment, key, expiration, delta, defaultValue, cas);
		}
Exemple #36
0
		public static IOperationResult Store(this IMemcachedClient self, StoreMode mode, string key, object value, Expiration expiration, ulong cas = Protocol.NO_CAS)
		{
			return self.StoreAsync(mode, key, value, expiration, cas).RunAndUnwrap();
		}
Exemple #37
0
		public static Task<IOperationResult> TouchAsync(this IMemcachedClient self, string key, Expiration expiration)
		{
			return self.TouchAsync(key, expiration, Protocol.NO_CAS);
		}
        public async Task <OperationResult> StoreWithResultAsync(StoreMode mode, string key, object value, ulong cas = 0, Expiration expiration = default)
        {
            try
            {
                using var sb = new SequenceBuilder(allocator);

                var flags = itemFormatter.Serialize(sb, value);
                var op    = PerformStore(mode, key, flags, sb, cas, expiration, silent: false);

                await cluster.Execute(op).ConfigureAwait(false);

                return(new OperationResult((OperationStatus)op.StatusCode, op.Cas));
            }
            catch (IOException e)
            {
                return(new OperationResult(e));
            }
        }
		public static IMutateOperationResult Mutate(this IMemcachedClient self, MutationMode mode, string key, Expiration expiration, ulong delta = Protocol.MUTATE_DEFAULT_DELTA, ulong defaultValue = Protocol.MUTATE_DEFAULT_VALUE, ulong cas = Protocol.NO_CAS)
		{
			return self.MutateAsync(mode, key, expiration, delta, defaultValue, cas).RunAndUnwrap();
		}
Exemple #40
0
 public static IOperationResult Add(this IMemcachedClient self, string key, object value, Expiration expiration, ulong cas = Protocol.NO_CAS)
 {
     return(self.Store(StoreMode.Add, key, value, expiration, cas));
 }
Exemple #41
0
 public static Task <IOperationResult> SetAsync(this IMemcachedClient self, string key, object value, Expiration expiration, ulong cas = Protocol.NO_CAS)
 {
     return(self.StoreAsync(StoreMode.Set, key, value, expiration, cas));
 }
Exemple #42
0
 public Task <IOperationResult> TouchAsync(string key, Expiration expiration, ulong cas)
 {
     return(PerformTouch(key, expiration, cas));
 }
		public static ulong Decrement(this ISimpleMemcachedClient self, string key, Expiration expiration, ulong delta = Protocol.MUTATE_DEFAULT_DELTA, ulong defaultValue = Protocol.MUTATE_DEFAULT_VALUE)
		{
			return self.Mutate(MutationMode.Decrement, key, expiration, delta, defaultValue);
		}
Exemple #44
0
 public Task <IMutateOperationResult> MutateAsync(MutationMode mode, string key, Expiration expiration, ulong delta, ulong defaultValue, ulong cas)
 {
     return(PerformMutate(mode, key, expiration, delta, defaultValue, cas));
 }
		public static Task<ulong> MutateAsync(this ISimpleMemcachedClient self, MutationMode mode, string key, Expiration expiration)
		{
			return self.MutateAsync(mode, key, expiration, Protocol.MUTATE_DEFAULT_DELTA, Protocol.MUTATE_DEFAULT_VALUE);
		}