caches

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 21, 2023 License: BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Overview

Caches Package

The `caches` package defines the use of caching within the dbFusion system. It has been developed to provide extensible and intelligent caching implementations.

**Cache Interface:** - As caching is a pluggable component within dbFusion, any cache implementation must adhere to the `CacheInterface`.

**Package Purpose:** - The primary purpose of this package is to provide the core architecture for caching and to offer an example implementation using Redis.

**Cache Processor:** - This package includes a cache processor that handles the intricacies of caching. - The cache processor performs the following tasks:

  • It processes keys and constructs composite indexes.
  • It searches for composite indexes and provides updates.
  • Deletion is straightforward, currently deleting the first index, while composite indexes invalidate on expiration or are moved away for LRU (Least Recently Used) eviction.

Developers can use this package as a foundation for creating their own custom cache solutions within the dbFusion framework. It provides the necessary architecture and an example Redis implementation for reference.

Please note that while this package offers a Redis-based cache example, developers are encouraged to tailor their cache implementations to meet specific use cases and requirements.

Index

Constants

This section is empty.

Variables

View Source
var CACHE_DEFAULT_CONNECTIONS = 1000
View Source
var CACHE_MAX_CONNECTIONS = 1000
View Source
var CACHE_MAX_IDLE_CONNECTIONS = 80
View Source
var CACHE_PARALLEL_PROCESS = 1000
View Source
var MAX_CACHE_SIZE = 1024

Cache Configuration Variables

This section documents the configuration variables related to caching in the dbFusion system. These variables control various aspects of cache behavior and usage.

1. MAX_CACHE_SIZE (int)

  • Description: Specifies the maximum size of the cache in units appropriate for your implementation.
  • Default Value: 1024
  • Usage: This variable sets the maximum capacity of the cache. It determines how much data the cache can store before older data is evicted to make room for new entries.

2. USE_CACHE (bool)

  • Description: Indicates whether caching is allowed or not.
  • Default Value: true
  • Usage: Set this variable to control whether dbFusion should use caching. If set to true, caching will be enabled, and queries may utilize cached data if available.

3. CACHE_DEFAULT_CONNECTIONS (int)

  • Description: Defines the default number of connections to the cache.
  • Default Value: 1000
  • Usage: This variable sets the default number of concurrent connections to the cache system. Adjust it based on the capacity and requirements of your cache implementation.

4. CACHE_MAX_IDLE_CONNECTIONS (int)

  • Description: Specifies the maximum number of idle (unused) cache connections.
  • Default Value: 80
  • Usage: Control the maximum number of cache connections that can remain idle without being closed. This optimization helps manage resource usage.

5. CACHE_MAX_CONNECTIONS (int)

  • Description: Sets the maximum number of total cache connections.
  • Default Value: 1000
  • Usage: Define the maximum limit for concurrent cache connections. Adjust this value according to the scalability and capacity needs of your application.

6. CACHE_PARALLEL_PROCESS (int)

  • Description: Determines how many parallel processes can connect to the cache at the same time.
  • Default Value: 1000
  • Usage: Set this variable to manage the number of simultaneous connections that can be established to the cache system. It influences the concurrency of cache-related operations.

These configuration variables allow you to fine-tune the behavior of the caching system within dbFusion, ensuring that it aligns with your application's requirements and resource constraints.

View Source
var USE_CACHE = true

Functions

func GetInstance

func GetInstance() *cacheProcessor

GetInstance returns a singleton instance of the cache processor.

Types

type Cache

type Cache interface {
	ConnectCache(connectionUri string, password ...string) error
	IsConnected() bool
	DisconnectCache()
	GetKey(key string) (interface{}, error)
	SetKey(key string, value interface{}) error
	FlushAll()
	DelKey(key string) error
}

Cache Interface

The Cache interface defines a contract for all caches that intend to be used with dbFusion. Any cache implementation must implement this interface to ensure compatibility with dbFusion's caching system.

Methods:

1. ConnectCache(connectionUri string, password ...string) error

  • Description: Establishes a connection to the cache using the provided connection URI and optional password.
  • Parameters:
  • `connectionUri` (string): The URI used to connect to the cache.
  • `password` (string, optional): A password, if required for cache authentication.
  • Returns:
  • `error`: An error if the connection cannot be established.

2. IsConnected() bool

  • Description: Checks if the cache is currently connected.
  • Returns:
  • `bool`: `true` if the cache is connected; otherwise, `false`.

3. DisconnectCache()

  • Description: Closes the connection to the cache gracefully.

4. GetKey(key string) (interface{}, error)

  • Description: Retrieves a value from the cache associated with the provided key.
  • Parameters:
  • `key` (string): The key used to identify the value in the cache.
  • Returns:
  • `interface{}`: The cached value.
  • `error`: An error if the value cannot be retrieved.

5. SetKey(key string, value interface{}) error

  • Description: Stores a value in the cache with the specified key.
  • Parameters:
  • `key` (string): The key to associate with the value in the cache.
  • `value` (interface{}): The value to be stored.
  • Returns:
  • `error`: An error if the value cannot be stored in the cache.

6. FlushAll()

  • Description: Clears all data stored in the cache. Use with caution as it removes all cached items.

7. DelKey(key string) error

  • Description: Deletes a key from the cache.
  • Parameters:
  • `key` (string): The key to be deleted from the cache.
  • Returns:
  • `error`: An error if the key cannot be deleted from the cache.

This interface provides a common set of methods that cache implementations must adhere to, allowing dbFusion to work seamlessly with various caching systems. Implement this interface to create a cache that can be used with dbFusion's caching capabilities.

type RedisCache

type RedisCache struct {
	// contains filtered or unexported fields
}

RedisCache struct implements the Cache interface for Redis.

func (*RedisCache) ConnectCache

func (rc *RedisCache) ConnectCache(connectionUri string, password ...string) error

ConnectCache establishes a connection to the Redis cache using the provided URI and optional password. It initializes the Redis connection pool if it doesn't exist. Parameters:

  • connectionUri (string): The URI used to connect to the Redis cache.
  • password (string, optional): An optional password for cache authentication.

Returns:

  • error: An error if the connection cannot be established.

func (*RedisCache) DelKey

func (rc *RedisCache) DelKey(key string) error

DelKey deletes a key from the Redis cache. It acquires a semaphore to ensure thread safety during the operation.

Parameters:

  • key (string): The key to be deleted from the cache.

Returns:

  • error: An error, if any, that occurred during the deletion.

func (*RedisCache) DisconnectCache

func (rc *RedisCache) DisconnectCache()

DisconnectCache closes the connection to the Redis cache gracefully.

func (*RedisCache) FlushAll

func (rc *RedisCache) FlushAll()

FlushAll clears all data stored in the Redis cache. Use with caution as it removes all cached items.

func (*RedisCache) GetKey

func (rc *RedisCache) GetKey(key string) (interface{}, error)

GetKey retrieves a value from the Redis cache associated with the provided key. Parameters:

  • key (string): The key used to identify the value in the cache.

Returns:

  • interface{}: The cached value.
  • error: An error if the value cannot be retrieved.

func (*RedisCache) IsConnected

func (rc *RedisCache) IsConnected() bool

IsConnected checks if the Redis cache is currently connected. Returns:

  • bool: `true` if the cache is connected; otherwise, `false`.

func (*RedisCache) SetKey

func (rc *RedisCache) SetKey(key string, value interface{}) error

SetKey stores a value in the Redis cache with the specified key. Parameters:

  • key (string): The key to associate with the value in the cache.
  • value (interface{}): The value to be stored.

Returns:

  • error: An error if the value cannot be stored in the cache.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL