A very simple and cross-platform implementation of the cache table framework that uses only use std::{ mem, sync::{Arc, Mutex, RwLock, TryLockError}, } set of the standard rust library
The num_shards table is divided into independent sections for optimization, the optimal value is num_shards = the number of cores on your processor for multithreaded operation. The redream_factor can be from 1.0 to 10.0, depending on how Mambo is planned to be applied. ->10.0 when a lot of elements are planned (more than 10_000 ). if there are few elements, about 1000 or less, it is better to use values tending to ->1.0. At ->10, memory consumption per element decreases (depending on the system, at 1.0, memory costs per element range from 32 to 64 bytes of overhead memory, at ->10.0 per 1 element costs are reduced to ~10 bytes per element)
Some of the Mambo structure data is thread-independent, and each copy via arc_clone provides a convenient instance of the Mambo structure that can be used in a new thread. all data regarding the elements of the Mambo hash table can be obtained from any stream that has an instance of arc_clone.
Attention!! DO NOT CALL read INSIDE THE read CLOSURE!!! THIS MAY CAUSE THE THREAD TO SELF-LOCK!!
reading. to access an item for reading, you need to request it using the key. and the element is read and processed in the RFy closure:FnOnce(Option<&mut T>) -> Result<(), &'static str>, since &mut T is Mutex.lock(). while processing is taking place in read< RFy >, the shard in which this element is located cannot: 1 change its size. 2: since Mutex contains elements from 0 to redream_factor (a parameter in the Mamdo::new constructor), access in other threads for elements in one Mutex is blocked. to summarize, the faster the closure is resolved inside read, the better.
to remove the fragment. you need to return the key key: u64 if an element was in the table and it was deleted, but the function returns Ok(Some(T.clone())) if there was no such element in the hash table, Ok(None) will be returned
insert an element T with the key: u64.if there is already an element with the same key: u64 in the table, then when force_replace == true, the old element T will be replaced by the new element T, while the old element T will be returned as Ok(Some(T.clone())). if force_replace == false, the old element will not be replaced and the function will return Ok(None). if there is no element with this key: u64, a new element will be added to the table and the function will output Ok(None)