Example #1
0
        public async Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options, CancellationToken token = default(CancellationToken))
        {
            var uri = UriFactory.CreateDocumentCollectionUri(_options.DatabaseName, _options.CollectionName);

            int ttl      = options.AbsoluteExpirationRelativeToNow.HasValue ? (int)options.AbsoluteExpirationRelativeToNow.Value.TotalSeconds : 10;
            var document = CosmosDbCacheItem.Build(key, ttl, value);

            await _client.UpsertDocumentAsync(uri, document);
        }
Example #2
0
        public async Task <byte[]> GetAsync(string key, CancellationToken token = default(CancellationToken))
        {
            var uri = this.GetDocumentUri();

            try
            {
                string      sql          = $"SELECT * FROM c WHERE c.id = '{key}'";
                FeedOptions queryOptions =
                    new FeedOptions
                {
                    MaxItemCount = -1,
                    EnableCrossPartitionQuery = false,
                    PopulateQueryMetrics      = true,
                    PartitionKey = new PartitionKey(key)
                };
                CosmosDbCacheItem result = _client.CreateDocumentQuery <CosmosDbCacheItem>(uri, queryOptions).Where(i => i.Id == key).AsEnumerable().First();

                return(result.ToByteContent());
            }
            catch (Exception d)
            {
                return(null);
            }
        }