/*
* Copyright (c) 2024 Vaughn Nugent
*
* Library: VNLib
* Package: VNLib.Data.Caching
* File: IGlobalCacheProvider.cs
*
* IGlobalCacheProvider.cs is part of VNLib.Data.Caching which is part of the larger
* VNLib collection of libraries and utilities.
*
* VNLib.Data.Caching is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* VNLib.Data.Caching is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
using System;
using System.Buffers;
using System.Threading;
using System.Threading.Tasks;
namespace VNLib.Data.Caching
{
///
/// A delegate method that will set the raw object data on the state object
/// if data was found
///
///
/// The state passed to the original call
/// The raw data of the cached object
public delegate void ObjectDataSet(T state, ReadOnlySpan objectData);
///
/// A delegate method that will get the raw object data from a state object
///
///
/// The state object passed to the caller
/// The raw object data to store in cache
public delegate ReadOnlySpan ObjectDataGet(T state);
///
/// A delegate method that will write the raw object data to the supplied
/// data buffer
///
///
/// The state object passed to the caller
/// The finite sized buffer writer use to write object data to
public delegate void ObjectDataReader(T state, IBufferWriter finiteWriter);
///
/// A delegate method that will get an object from the raw object data
///
///
///
/// Optional user-state data
/// The object data to compute the object result from
/// The resultant object
public delegate TObject GetObjectFromData(TState state, ReadOnlySpan data);
///
/// Internal structure used to store a callback and state for the
/// a data read/get operation on a cache object
///
///
/// The user-state object to pass
/// The data get callback function
internal readonly record struct ObjectDataGetState(T UserState, ObjectDataGet Getter);
internal sealed class GetObjectState(TState State, GetObjectFromData Getter)
{
public T? Result;
public void ComputeResult(ReadOnlySpan data) => Result = Getter(State, data);
}
///
/// A global cache provider interface
///
public interface IGlobalCacheProvider
{
///
/// Gets a value that indicates if the cache provider is currently available
///
bool IsConnected { get; }
///
/// Gets the underlying cache store object
///
/// The underlying cache store instance
object GetUnderlyingStore();
///
/// Gets the default deserializer for the cache provider
///
ICacheObjectDeserializer DefaultDeserializer { get; }
///
/// Gets the default serializer for the cache provider
///
ICacheObjectSerializer DefaultSerializer { get; }
///
/// Asynchronously deletes an item from cache by its key
///
/// The key identifying the item to delete
/// A token to cancel the async operation
/// A task that completes when the delete operation has compelted
Task DeleteAsync(string key, CancellationToken cancellation);
///
/// Asynchronously gets a value from the backing cache store
///
///
/// The key identifying the object to recover from cache
/// The specific deserialzer to deserialze the object
/// A token to cancel the async operation
/// The value if found, or null if it does not exist in the store
Task GetAsync(string key, ICacheObjectDeserializer deserializer, CancellationToken cancellation);
///
/// Asynchronously sets (or updates) a cached value in the backing cache store
///
///
/// The key identifying the object to recover from cache
/// An optional key that will be changed for the new object
/// A token to cancel the async operation
/// The value to set at the given key
/// The used to serialze the entity
/// A task that completes when the update operation has compelted
Task AddOrUpdateAsync(string key, string? newKey, T value, ICacheObjectSerializer serialzer, CancellationToken cancellation);
///
/// Asynchronously gets a value from the backing cache store and writes it to the
/// supplied data buffer
///
/// The key identifying the object to recover from cache
/// The callback method that will get the raw object data
/// The state parameter to pass to the callback when invoked
/// A token to cancel the async operation
/// A task that complets when the object data has been written to the data buffer
Task GetAsync(string key, ObjectDataSet callback, T state, CancellationToken cancellation);
///
/// Asynchronously sets (or updates) a cached value in the backing cache store
/// from the supplied raw data
///
/// The key identifying the object to recover from cache
/// An optional key that will be changed for the new object
/// A callback method that will set the raw object data when received
/// The callback state parameter
/// A token to cancel the async operation
/// A task that completes when the update operation has compelted
Task AddOrUpdateAsync(string key, string? newKey, ObjectDataGet callback, T state, CancellationToken cancellation);
}
}