This README provides a step-by-step explanation of the cache system implemented in Go. It includes an overview of the logic and detailed descriptions of each function.
The cache system is based on a Least Recently Used (LRU) strategy. The primary components are:
- Cache: Maintains the queue and hash map.
- Queue: A doubly linked list for tracking the order of elements.
- Hash: A map for quick access to elements by their value.
- Efficient O(1) operations for adding, removing, and accessing elements.
- Dynamic cache size management.
- Functions for cache inspection and manipulation.
Creates a new cache with a specified maximum size.
size int: The maximum number of elements the cache can hold.
Cache: An initialized cache instance.
c := NewCache(5) // Creates a cache with a maximum size of 5Adds a value to the cache or refreshes its position if it already exists.
str string: The value to be added or refreshed.
- If the value exists in the hash, remove it to refresh its position.
- Otherwise, create a new node for the value.
- Add the node to the front of the queue.
- Update the hash map.
c.Check("abc")Adds a new node to the front of the queue.
n *Node: The node to be added.
- Adjust the links in the queue to insert the node at the front.
- Increment the queue length.
- If the queue exceeds the maximum size, remove the least recently used node.
n := &Node{Val: "abc"}
c.Add(node)Removes a node from the cache.
n *Node: The node to be removed.
- Update the links of the node’s neighbors to exclude it.
- Decrement the queue length.
- Remove the value from the hash map.
*Node: The removed node.
r := c.Remove(node)Prints the current state of the cache.
- Traverse the queue from the head to the tail.
- Print each value in order.
c.Display()Removes all elements from the cache.
- Reinitialize the queue.
- Clear the hash map.
c.Clear()Checks if a value exists in the cache.
str string: The value to check.
bool:trueif the value exists,falseotherwise.
exists := c.Exists("abc")Retrieves a value from the cache without removing it.
str string: The value to retrieve.
string: The value if it exists.bool:trueif the value exists,falseotherwise.
val, found := c.Get("abc")Returns the current number of elements in the cache.
int: The length of the cache.
length := c.Length()Returns the most recently used value without removing it.
string: The value of the most recently used item.bool:trueif the cache is not empty,falseotherwise.
val, found := c.Peek()Updates an existing value in the cache.
old string: The value to be updated.new string: The new value.
bool:trueif the update was successful,falseotherwise.
updated := c.Update("old", "new")Retrieves all keys currently in the cache.
[]string: A slice of all keys.
keys := c.Keys()Retrieves all values currently in the cache.
[]string: A slice of all values.
values := c.Values()Checks if the cache has reached its maximum capacity.
bool:trueif the cache is full,falseotherwise.
isFull := c.IsFull()Removes the least recently used item from the cache.
- If the cache is not empty, remove the last node in the queue.
c.RemoveLeastRecentlyUsed()Moves a specific value to the most recently used position.
val string: The value to promote.
bool:trueif the promotion was successful,falseotherwise.
pmt := c.Promote("abc")Updates the maximum size of the cache.
size int: The new maximum size.
- Update the size attribute.
- Remove least recently used items until the cache size is within the new limit.
c.SetMaxSize(10)- None.
- Iterates through the queue and prints each element's value.
- None.
q.Display()val string: The value to add to the queue.
- Adds a new node to the end of the queue.
- None.
q.Enqueue("example")- None.
- Removes and returns the value at the front of the queue.
(string, bool): The value and a boolean indicating if the queue was non-empty.
val, success := q.Dequeue()- None.
- Checks if the queue is empty.
bool: True if empty, false otherwise.
empty := q.IsEmpty()- None.
- Returns the value at the front of the queue without removing it.
(string, bool): The value and a boolean indicating if the queue is non-empty.
val, success := q.Peek()- None.
- Removes all elements from the queue.
- None.
q.Clear()val string: The value to check in the queue.
- Checks if the value exists in the queue.
bool: True if the value exists, false otherwise.
exists := q.Contains("example")- None.
- Returns
val string: The value to check in the queue.
- Removes a specific value from the queue.
- Finds the node containing the value, adjusts its neighbors' links, and decreases the length.
bool: True if the value exists, false otherwise.
rm := q.Remove("B")- None.
- Reverses the order of the elements in the queue.
- Swaps the left and right pointers of all nodes and exchanges the head and tail links.
- None.
q.Reverse()- None.
- Converts the queue into a slice of strings.
- Iterates through the queue and appends each value to a slice.
[]string: The slice containing all queue elements.
slice := q.ToSlice()