aboutsummaryrefslogtreecommitdiff
path: root/lib/VNLib.Data.Caching.ObjectCache/src/IBlobCache.cs
blob: bc3180b8eea901d3db45326d36385879643bca5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* Copyright (c) 2023 Vaughn Nugent
* 
* Library: VNLib
* Package: VNLib.Data.Caching.ObjectCache
* File: IBlobCache.cs 
*
* IBlobCache.cs is part of VNLib.Data.Caching.ObjectCache which is part of the larger 
* VNLib collection of libraries and utilities.
*
* VNLib.Data.Caching.ObjectCache 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.ObjectCache 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.Collections.Generic;

using VNLib.Utils.Memory;

namespace VNLib.Data.Caching.ObjectCache
{
    /// <summary>
    /// Represents a binary data cache store
    /// </summary>
    public interface IBlobCache : IEnumerable<KeyValuePair<string, CacheEntry>>, IDisposable
    {
        /// <summary>
        /// The id of the bucket this memory cache belongs to
        /// </summary>
        public uint BucketId { get; }

        /// <summary>
        /// The internal heap used to allocate <see cref="CacheEntry"/> buffers
        /// </summary>
        IUnmangedHeap CacheHeap { get; }

        /// <summary>
        /// Attempts to retreive the entry at the given id.
        /// </summary>
        /// <param name="objectId">The id of the object to locate</param>
        /// <param name="entry">The cache entry if found, default otherwise</param>
        /// <returns>True if the entry was assigned</returns>
        bool TryGetValue(string objectId, out CacheEntry entry);

        /// <summary>
        /// Attempts to relocate the entry in the table by its new id.
        /// </summary>
        /// <param name="objectId">The original id of the entry to modify</param>
        /// <param name="newId">The new id of the entry</param>
        /// <param name="entry">The original entry if found, default otherwise</param>
        /// <returns>True if the item was located and successfully updated, false if the operation failed</returns>
        bool TryChangeKey(string objectId, string newId, out CacheEntry entry);

        /// <summary>
        /// Adds the entry to the table by the id
        /// </summary>
        /// <param name="objectId"></param>
        /// <param name="entry">The entry to store in the table</param>
        void Add(string objectId, CacheEntry entry);

        /// <summary>
        /// Attempts to remove the entry at the given id, and returns the 
        /// entry if located.
        /// </summary>
        /// <param name="objectId">The id of the entry to remove</param>
        /// <param name="entry">The entry if found, default otherwise</param>
        /// <returns>True if the entry existed in the store, false otherwise</returns>
        /// <remarks>
        /// NOTE: If the return value is true, the store no longer maintains the lifetime
        /// of the returned <see cref="CacheEntry"/>. You must manually dispose the entry 
        /// to avoid memory leaks.
        /// </remarks>
        bool Remove(string objectId, out CacheEntry entry);

        /// <summary>
        /// Attempts to remove the entry at the given id, and release its memory.
        /// </summary>
        /// <param name="objectId">The id of the entry to remove</param>
        /// <returns>True if the entry was found and disposed</returns>
        bool Remove(string objectId);
    }
}