
Security News
npm Adopts OIDC for Trusted Publishing in CI/CD Workflows
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
github.com/CrisisTextLine/modular/modules/cache
The Cache Module provides caching functionality for Modular applications. It offers different cache backend options including in-memory and Redis (placeholder implementation).
import (
"github.com/CrisisTextLine/modular"
"github.com/CrisisTextLine/modular/modules/cache"
)
// Register the cache module with your Modular application
app.RegisterModule(cache.NewModule())
The cache module can be configured using the following options:
cache:
engine: memory # Cache engine to use: "memory" or "redis"
defaultTTL: 300 # Default TTL in seconds if not specified (300s = 5 minutes)
cleanupInterval: 60 # How often to clean up expired items (60s = 1 minute)
maxItems: 10000 # Maximum items to store in memory cache
redisURL: "" # Redis connection URL (for Redis engine)
redisPassword: "" # Redis password (for Redis engine)
redisDB: 0 # Redis database number (for Redis engine)
connectionMaxAge: 60 # Maximum age of connections in seconds
// In your module's Init function
func (m *MyModule) Init(app modular.Application) error {
var cacheService *cache.CacheModule
err := app.GetService("cache.provider", &cacheService)
if err != nil {
return fmt.Errorf("failed to get cache service: %w", err)
}
// Now you can use the cache service
m.cache = cacheService
return nil
}
// Define the service dependency
func (m *MyModule) RequiresServices() []modular.ServiceDependency {
return []modular.ServiceDependency{
{
Name: "cache",
Required: true,
MatchByInterface: true,
SatisfiesInterface: reflect.TypeOf((*cache.CacheEngine)(nil)).Elem(),
},
}
}
// Access the service in your constructor
func (m *MyModule) Constructor() modular.ModuleConstructor {
return func(app modular.Application, services map[string]any) (modular.Module, error) {
cacheService := services["cache"].(cache.CacheEngine)
return &MyModule{cache: cacheService}, nil
}
}
// Set a value in the cache with a TTL
err := cacheService.Set(ctx, "user:123", userData, 5*time.Minute)
if err != nil {
// Handle error
}
// Get a value from the cache
value, found := cacheService.Get(ctx, "user:123")
if !found {
// Cache miss, fetch from primary source
} else {
userData = value.(UserData)
}
// Delete a value from the cache
err := cacheService.Delete(ctx, "user:123")
if err != nil {
// Handle error
}
// Get multiple values
keys := []string{"user:123", "user:456", "user:789"}
results, err := cacheService.GetMulti(ctx, keys)
if err != nil {
// Handle error
}
// Set multiple values
items := map[string]interface{}{
"user:123": userData1,
"user:456": userData2,
}
err := cacheService.SetMulti(ctx, items, 10*time.Minute)
if err != nil {
// Handle error
}
// Delete multiple values
err := cacheService.DeleteMulti(ctx, keys)
if err != nil {
// Handle error
}
The cache module includes comprehensive tests for the memory cache implementation.
FAQs
Unknown package
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.
Security News
The CNA Scorecard ranks CVE issuers by data completeness, revealing major gaps in patch info and software identifiers across thousands of vulnerabilities.