internal bool SetInternal(string key, TableCacheEntry entry)
        {
            TableOperation op     = TableOperation.InsertOrReplace(entry);
            TableResult    result = table.Execute(op);

            return(result.HttpStatusCode == 200 || result.HttpStatusCode == 204);    // Success or "No content"
        }
        public bool Set <T>(string key, T value)
        {
            string          sVal  = Serialize <T>(value);
            TableCacheEntry entry = CreateTableEntry(key, sVal);

            return(SetInternal(key, entry));
        }
        public bool AddInternal(string key, TableCacheEntry entry)
        {
            TableOperation op     = TableOperation.Insert(entry);
            TableResult    result = table.Execute(op);

            return(result.HttpStatusCode == 200);
        }
Beispiel #4
0
        public async Task <bool> AddInternalAsync(string key, TableCacheEntry entry, CancellationToken token = default)
        {
            var op     = TableOperation.Insert(entry);
            var result = await table.ExecuteAsync(op, token);

            return(result.HttpStatusCode == 200);
        }
        public bool Add <T>(string key, T value, DateTime expiresAt)
        {
            string sVal = serializer.SerializeToString <T>(value);

            TableCacheEntry entry = CreateTableEntry(key, sVal, null, expiresAt);

            return(AddInternal(key, entry));
        }
        public bool Set <T>(string key, T value, DateTime expiresAt)
        {
            string sVal = Serialize <T>(value);

            TableCacheEntry entry = CreateTableEntry(key, sVal, null, expiresAt);

            return(SetInternal(key, entry));
        }
        public T Get <T>(string key)
        {
            TableCacheEntry entry = GetEntry(key);

            if (entry != null)
            {
                return(Deserialize <T>(entry.Data));
            }
            return(default(T));
        }
        internal bool ReplaceInternal(string key, string value, DateTime?expiresAt = null)
        {
            TableCacheEntry entry = null;

            if (TryGetValue(key, out entry))
            {
                entry = CreateTableEntry(key, value, null, expiresAt);
                TableOperation op     = TableOperation.Replace(entry);
                TableResult    result = table.Execute(op);
                return(result.HttpStatusCode == 200);
            }
            return(false);
        }
        public TimeSpan?GetTimeToLive(string key)
        {
            TableCacheEntry entry = GetEntry(key);

            if (entry != null)
            {
                if (entry.ExpiryDate == null)
                {
                    return(TimeSpan.MaxValue);
                }

                return(entry.ExpiryDate - DateTime.UtcNow);
            }
            return(null);
        }
        internal TableCacheEntry GetEntry(string key)
        {
            TableCacheEntry entry = null;

            if (TryGetValue(key, out entry))
            {
                if (entry.HasExpired)
                {
                    this.Remove(key);
                    return(null);
                }
                return(entry);
            }
            return(null);
        }
Beispiel #11
0
        private bool TryGetValue(string key, out TableCacheEntry entry)
        {
            entry = null;

            var op = TableOperation.Retrieve <TableCacheEntry>(partitionKey, key);
            var retrievedResult = table.Execute(op);

            if (retrievedResult.Result != null)
            {
                entry = retrievedResult.Result as TableCacheEntry;
                return(true);
            }

            return(false);
        }
        public bool Remove(string key)
        {
            TableCacheEntry entry = CreateTableEntry(key);

            entry.ETag = "*";   // Avoids concurrency
            TableOperation op = TableOperation.Delete(entry);

            try
            {
                TableResult result = table.Execute(op);
                return(result.HttpStatusCode == 200 || result.HttpStatusCode == 204);
            }
            catch (Microsoft.WindowsAzure.Storage.StorageException ex)
            {
                if (ex.RequestInformation.HttpStatusCode == (int)System.Net.HttpStatusCode.NotFound)
                {
                    return(false);
                }
                throw ex;
            }
        }