Headless CMS and Content Managment Hub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

102 lines
2.9 KiB

// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using Microsoft.Extensions.Caching.Distributed;
using MongoDB.Driver;
namespace Squidex.Infrastructure.Caching;
public sealed class MongoDistributedCache(IMongoDatabase database) : MongoRepositoryBase<MongoCacheEntry>(database), IDistributedCache
{
protected override string CollectionName()
{
return "Cache";
}
protected override Task SetupCollectionAsync(IMongoCollection<MongoCacheEntry> collection,
CancellationToken ct)
{
return Collection.Indexes.CreateOneAsync(
new CreateIndexModel<MongoCacheEntry>(
Index.Ascending(x => x.Expires),
new CreateIndexOptions
{
ExpireAfter = TimeSpan.Zero
}),
null, ct);
}
public byte[] Get(string key)
{
throw new NotSupportedException();
}
public void Refresh(string key)
{
throw new NotSupportedException();
}
public void Remove(string key)
{
throw new NotSupportedException();
}
public void Set(string key, byte[] value, DistributedCacheEntryOptions options)
{
throw new NotSupportedException();
}
public Task RefreshAsync(string key,
CancellationToken token = default)
{
return Task.CompletedTask;
}
public Task RemoveAsync(string key,
CancellationToken token = default)
{
return Collection.DeleteOneAsync(x => x.Key == key, token);
}
public async Task<byte[]?> GetAsync(string key,
CancellationToken token = default)
{
var entry = await Collection.Find(x => x.Key == key).FirstOrDefaultAsync(token);
if (entry != null && entry.Expires > DateTime.UtcNow)
{
return entry.Value;
}
return null;
}
public Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options,
CancellationToken token = default)
{
var expires = DateTime.UtcNow;
if (options.AbsoluteExpiration.HasValue)
{
expires = options.AbsoluteExpiration.Value.UtcDateTime;
}
else if (options.AbsoluteExpirationRelativeToNow.HasValue)
{
expires += options.AbsoluteExpirationRelativeToNow.Value;
}
else if (options.SlidingExpiration.HasValue)
{
expires += options.SlidingExpiration.Value;
}
return Collection.UpdateOneAsync(x => x.Key == key,
Update
.Set(x => x.Value, value)
.Set(x => x.Expires, expires),
Upsert, token);
}
}