Managing Cache Values
The CacheService provides comprehensive methods for managing cache values programmatically, including fetching, setting, and clearing cache entries.
Quick Start
- TypeScript
- Java
- Go
- .NET
import Ductape from '@ductape/sdk';
const ductape = new Ductape({
accessKey: 'your-access-key',
});
const { caches } = ductape;
// Fetch cache values
const values = await caches.fetchValues({
cache: 'user-cache',
});
// Get a specific value
const value = await cache.get({ key: 'user:123' });
// Set a value
await cache.set({
cache: 'user-cache',
key: 'user:123',
value: JSON.stringify({ name: 'John' }),
});
// Clear a value
await cache.clear({ key: 'user:123' });
import app.ductape.sdk.Ductape;
import app.ductape.sdk.core.EnvType;
import app.ductape.sdk.core.RequestContext;
RequestContext auth = new RequestContext(null, null, null, null, 'your-access-key');
Ductape ductape = new Ductape(EnvType.PRODUCTION, auth);
Map<String, Object> Map.of( caches ) = ductape;
// Fetch cache values
Map<String, Object> values = caches.fetchValues(Map.of(
"cache", "user-cache"
));
// Get a specific value
Map<String, Object> value = cache.get(Map.of( "key", ""user", 123" ));
// Set a value
cache.set(Map.of(
"cache", "user-cache",
"key", ""user", 123",
value: JSON.stringify(Map.of( "name", "John" ))
));
// Clear a value
cache.clear(Map.of( "key", ""user", 123" ));
import (
"context"
"github.com/ductape/ductape/sdk/go/core"
ductapesdk "github.com/ductape/ductape/sdk/go/ductape"
)
auth := core.NewRequestContext("", "", "", "", 'your-access-key')
client, err := ductapesdk.New(core.EnvProduction, auth)
if err != nil {
return err
}
const { caches } = ductape;
// Fetch cache values
values := caches.fetchValues({
"cache": "user-cache",
});
// Get a specific value
value := cache.get({ "key": ""user": 123" });
// Set a value
cache.set({
"cache": "user-cache",
"key": ""user": 123",
value: JSON.stringify({ "name": "John" }),
});
// Clear a value
cache.clear({ "key": ""user": 123" });
using Ductape.Sdk;
using Ductape.Sdk.Core;
var auth = new RequestContext(null, null, null, null, 'your-access-key', null);
var ductape = new Ductape(EnvType.Production, auth);
var { caches } = ductape;
// Fetch cache values
var values = await caches.fetchValues({
["cache"] = "user-cache",
});
// Get a specific value
var value = await cache.get({ ["key"] = "["user"] = 123" });
// Set a value
await cache.set({
["cache"] = "user-cache",
["key"] = "["user"] = 123",
value: JSON.stringify({ ["name"] = "John" }),
});
// Clear a value
await cache.clear({ ["key"] = "["user"] = 123" });
Fetching Cache Values
Retrieve paginated cache values with optional filtering by environment:
- TypeScript
- Java
- Go
- .NET
const result = await caches.fetchValues({
cache: 'user-cache',
page: 1,
limit: 20,
});
console.log('Total values:', result.total);
console.log('Total pages:', result.totalPages);
for (const value of result.values) {
console.log('Key:', value.key);
console.log('Value:', value.value);
console.log('Expires:', value.expiry);
}
Map<String, Object> result = caches.fetchValues(Map.of(
"cache", "user-cache",
"page", 1,
"limit", 20
));
System.out.println('Total "values", ", result.total);
System.out.println("Total "pages", ", result.totalPages);
for (Map<String, Object> value of result.values) Map.of(
System.out.println(""Key", ", value.key);
System.out.println(""Value", ", value.value);
System.out.println("Expires:', value.expiry);
)
result := caches.fetchValues({
"cache": "user-cache",
"page": 1,
"limit": 20,
});
fmt.Println('Total "values": ", result.total);
fmt.Println("Total "pages": ", result.totalPages);
for (const value of result.values) {
fmt.Println(""Key": ", value.key);
fmt.Println(""Value": ", value.value);
fmt.Println("Expires:', value.expiry);
}
var result = await caches.fetchValues({
["cache"] = "user-cache",
["page"] = 1,
["limit"] = 20,
});
Console.WriteLine('Total ["values"] = ", result.total);
Console.WriteLine("Total ["pages"] = ", result.totalPages);
for (var value of result.values) {
Console.WriteLine("["Key"] = ", value.key);
Console.WriteLine("["Value"] = ", value.value);
Console.WriteLine("Expires:', value.expiry);
}
Parameters
| Field | Type | Required | Description |
|---|---|---|---|
product | string | Yes | The tag of your product |
cache | string | Yes | The tag of the cache |
env | string | No | Filter by environment |
page | number | No | Page number (default: 1) |
limit | number | No | Values per page (default: 20) |
Response
- TypeScript
- Java
- Go
- .NET
{
values: ICacheValue[]; // Array of cache values
total: number; // Total number of values
page: number; // Current page
limit: number; // Values per page
totalPages: number; // Total number of pages
}
Map.of(
values: ICacheValue[]; // Array of cache values
total: number; // Total number of values
page: number; // Current page
limit: number; // Values per page
totalPages: number; // Total number of pages
)
{
values: ICacheValue[]; // Array of cache values
total: number; // Total number of values
page: number; // Current page
limit: number; // Values per page
totalPages: number; // Total number of pages
}
{
values: ICacheValue[]; // Array of cache values
total: number; // Total number of values
page: number; // Current page
limit: number; // Values per page
totalPages: number; // Total number of pages
}
Getting a Single Value
Retrieve a specific cache value by its key:
- TypeScript
- Java
- Go
- .NET
const value = await cache.get({ key: 'user:123' });
if (value) {
console.log('Found:', value.key);
console.log('Data:', JSON.parse(value.value));
} else {
console.log('Value not found');
}
Map<String, Object> value = cache.get(Map.of( "key", ""user", 123" ));
if (value) Map.of(
System.out.println('"Found", ", value.key);
System.out.println(""Data", ", JSON.parse(value.value));
) else Map.of(
System.out.println("Value not found');
)
value := cache.get({ "key": ""user": 123" });
if (value) {
fmt.Println('"Found": ", value.key);
fmt.Println(""Data": ", JSON.parse(value.value));
} else {
fmt.Println("Value not found');
}
var value = await cache.get({ ["key"] = "["user"] = 123" });
if (value) {
Console.WriteLine('["Found"] = ", value.key);
Console.WriteLine("["Data"] = ", JSON.parse(value.value));
} else {
Console.WriteLine("Value not found');
}
Parameters
| Field | Type | Required | Description |
|---|---|---|---|
key | string | Yes | The cache entry key |
Response
Returns ICacheValue | null:
- TypeScript
- Java
- Go
- .NET
{
_id: string; // Unique ID
key: string; // Cache key
value: string; // Cached value (stringified)
cache_tag: string; // Cache tag
product_tag: string; // Product tag
component_tag: string; // Component tag
component_type: string;// Component type
expiry?: Date; // Expiration date (if set)
createdAt: Date; // Creation timestamp
updatedAt: Date; // Last update timestamp
}
Map.of(
_id: string; // Unique ID
key: string; // Cache key
value: string; // Cached value (stringified)
cache_tag: string; // Cache tag
product_tag: string; // Product tag
component_tag: string; // Component tag
component_type: string;// Component type
expiry?: Date; // Expiration date (if set)
createdAt: Date; // Creation timestamp
updatedAt: Date; // Last update timestamp
)
{
_id: string; // Unique ID
key: string; // Cache key
value: string; // Cached value (stringified)
cache_tag: string; // Cache tag
product_tag: string; // Product tag
component_tag: string; // Component tag
component_type: string;// Component type
expiry?: Date; // Expiration date (if set)
createdAt: Date; // Creation timestamp
updatedAt: Date; // Last update timestamp
}
{
_id: string; // Unique ID
key: string; // Cache key
value: string; // Cached value (stringified)
cache_tag: string; // Cache tag
product_tag: string; // Product tag
component_tag: string; // Component tag
component_type: string;// Component type
expiry?: Date; // Expiration date (if set)
createdAt: Date; // Creation timestamp
updatedAt: Date; // Last update timestamp
}
Setting Cache Values
Store a value in the cache:
- TypeScript
- Java
- Go
- .NET
await cache.set({
cache: 'user-cache',
key: 'user:123',
value: JSON.stringify({ name: 'John', email: 'john@example.com' }),
expiry: new Date(Date.now() + 3600000), // 1 hour from now
});
cache.set(Map.of(
"cache", "user-cache",
"key", ""user", 123",
value: JSON.stringify(Map.of( "name", "John", "email", "john@example.com" )),
expiry: new Date(Date.now() + 3600000), // 1 hour from now
));
cache.set({
"cache": "user-cache",
"key": ""user": 123",
value: JSON.stringify({ "name": "John", "email": "john@example.com" }),
expiry: new Date(Date.now() + 3600000), // 1 hour from now
});
await cache.set({
["cache"] = "user-cache",
["key"] = "["user"] = 123",
value: JSON.stringify({ ["name"] = "John", ["email"] = "john@example.com" }),
expiry: new Date(Date.now() + 3600000), // 1 hour from now
});
Parameters
| Field | Type | Required | Description |
|---|---|---|---|
product | string | Yes | The tag of your product |
cache | string | Yes | The tag of the cache |
key | string | Yes | Unique key for this entry |
value | string | Yes | The value to cache (typically JSON) |
componentTag | string | No | Component tag (defaults to cache tag) |
componentType | string | No | Component type (defaults to 'cache') |
expiry | Date | No | Expiration date |
Response
Returns boolean indicating success.
Clearing Cache Values
Clear a Single Value
Remove a specific cache entry by key:
- TypeScript
- Java
- Go
- .NET
const cleared = await cache.clear({ key: 'user:123' });
console.log('Cleared:', cleared);
Map<String, Object> cleared = cache.clear(Map.of( "key", ""user", 123" ));
System.out.println('Cleared:', cleared);
cleared := cache.clear({ "key": ""user": 123" });
fmt.Println('Cleared:', cleared);
var cleared = await cache.clear({ ["key"] = "["user"] = 123" });
Console.WriteLine('Cleared:', cleared);
Clear All Values
Remove all cache values for a product/cache combination:
- TypeScript
- Java
- Go
- .NET
const result = await caches.clearAll({
cache: 'user-cache',
});
console.log('Cleared', result.cleared, 'values');
Map<String, Object> result = caches.clearAll(Map.of(
"cache", "user-cache"
));
System.out.println('Cleared', result.cleared, 'values');
result := caches.clearAll({
"cache": "user-cache",
});
fmt.Println('Cleared', result.cleared, 'values');
var result = await caches.clearAll({
["cache"] = "user-cache",
});
Console.WriteLine('Cleared', result.cleared, 'values');
Fetching Dashboard Metrics
Get comprehensive metrics about your cache:
- TypeScript
- Java
- Go
- .NET
const dashboard = await caches.fetchDashboard({
cache: 'user-cache',
});
console.log('Total values:', dashboard.totalValues);
console.log('Active values:', dashboard.activeValues);
console.log('Expired values:', dashboard.expiredValues);
console.log('Total size:', dashboard.totalSize, 'bytes');
Map<String, Object> dashboard = caches.fetchDashboard(Map.of(
"cache", "user-cache"
));
System.out.println('Total "values", ", dashboard.totalValues);
System.out.println("Active "values", ", dashboard.activeValues);
System.out.println("Expired "values", ", dashboard.expiredValues);
System.out.println("Total "size", ", dashboard.totalSize, "bytes');
dashboard := caches.fetchDashboard({
"cache": "user-cache",
});
fmt.Println('Total "values": ", dashboard.totalValues);
fmt.Println("Active "values": ", dashboard.activeValues);
fmt.Println("Expired "values": ", dashboard.expiredValues);
fmt.Println("Total "size": ", dashboard.totalSize, "bytes');
var dashboard = await caches.fetchDashboard({
["cache"] = "user-cache",
});
Console.WriteLine('Total ["values"] = ", dashboard.totalValues);
Console.WriteLine("Active ["values"] = ", dashboard.activeValues);
Console.WriteLine("Expired ["values"] = ", dashboard.expiredValues);
Console.WriteLine("Total ["size"] = ", dashboard.totalSize, "bytes');
Response
- TypeScript
- Java
- Go
- .NET
{
totalValues: number; // Total number of cached values
activeValues: number; // Non-expired values
expiredValues: number; // Expired values
totalSize: number; // Total size in bytes
}
Map.of(
totalValues: number; // Total number of cached values
activeValues: number; // Non-expired values
expiredValues: number; // Expired values
totalSize: number; // Total size in bytes
)
{
totalValues: number; // Total number of cached values
activeValues: number; // Non-expired values
expiredValues: number; // Expired values
totalSize: number; // Total size in bytes
}
{
totalValues: number; // Total number of cached values
activeValues: number; // Non-expired values
expiredValues: number; // Expired values
totalSize: number; // Total size in bytes
}
Complete Example
Here's a complete example showing how to manage cache values:
- TypeScript
- Java
- Go
- .NET
import { CacheService } from '@ductape/sdk';
const cache = new CacheService({
accessKey: 'your-access-key',
});
async function manageCacheValues() {
const product = 'my-product';
const cacheTag = 'user-cache';
// Store some values
await cache.set({
product,
cache: cacheTag,
key: 'user:1',
value: JSON.stringify({ name: 'Alice', role: 'admin' }),
expiry: new Date(Date.now() + 86400000), // 24 hours
});
await cache.set({
product,
cache: cacheTag,
key: 'user:2',
value: JSON.stringify({ name: 'Bob', role: 'user' }),
expiry: new Date(Date.now() + 86400000),
});
// Get dashboard metrics
const dashboard = await cache.fetchDashboard({ product, cache: cacheTag });
console.log('Cache Stats:', dashboard);
// Fetch all values
const result = await cache.fetchValues({ product, cache: cacheTag });
console.log('Total values:', result.total);
// Get a specific value
const user = await cache.get({ key: 'user:1' });
if (user) {
console.log('User 1:', JSON.parse(user.value));
}
// Clear a value
await cache.clear({ key: 'user:2' });
// Clear all values
const cleared = await cache.clearAll({ product, cache: cacheTag });
console.log('Cleared', cleared.cleared, 'values');
}
manageCacheValues();
import Map.of( CacheService ) from '@ductape/sdk';
Map<String, Object> cache = new CacheService(Map.of(
"accessKey", "your-access-key"
));
async function manageCacheValues() Map.of(
Map<String, Object> product = 'my-product';
Map<String, Object> cacheTag = 'user-cache';
// Store some values
cache.set(Map.of(
product,
cache: cacheTag,
"key", ""user", 1",
value: JSON.stringify(Map.of( "name", "Alice", "role", "admin" )),
expiry: new Date(Date.now() + 86400000), // 24 hours
));
cache.set(Map.of(
product,
cache: cacheTag,
"key", ""user", 2",
value: JSON.stringify(Map.of( "name", "Bob", "role", "user" )),
expiry: new Date(Date.now() + 86400000)
));
// Get dashboard metrics
Map<String, Object> dashboard = cache.fetchDashboard(Map.of( product, cache: cacheTag ));
System.out.println('Cache "Stats", ", dashboard);
// Fetch all values
Map<String, Object> result = cache.fetchValues(Map.of( product, cache: cacheTag ));
System.out.println("Total "values", ", result.total);
// Get a specific value
Map<String, Object> user = cache.get(Map.of( "key", ""user", 1' ));
if (user) Map.of(
System.out.println('User "1", ", JSON.parse(user.value));
)
// Clear a value
cache.clear(Map.of( key: ""user", 2' ));
// Clear all values
Map<String, Object> cleared = cache.clearAll(Map.of( product, cache: cacheTag ));
System.out.println('Cleared', cleared.cleared, 'values');
)
manageCacheValues();
import { CacheService } from '@ductape/sdk';
cache := new CacheService({
"accessKey": "your-access-key",
});
async function manageCacheValues() {
product := 'my-product';
cacheTag := 'user-cache';
// Store some values
cache.set({
product,
cache: cacheTag,
"key": ""user": 1",
value: JSON.stringify({ "name": "Alice", "role": "admin" }),
expiry: new Date(Date.now() + 86400000), // 24 hours
});
cache.set({
product,
cache: cacheTag,
"key": ""user": 2",
value: JSON.stringify({ "name": "Bob", "role": "user" }),
expiry: new Date(Date.now() + 86400000),
});
// Get dashboard metrics
dashboard := cache.fetchDashboard({ product, cache: cacheTag });
fmt.Println('Cache "Stats": ", dashboard);
// Fetch all values
result := cache.fetchValues({ product, cache: cacheTag });
fmt.Println("Total "values": ", result.total);
// Get a specific value
user := cache.get({ "key": ""user": 1' });
if (user) {
fmt.Println('User "1": ", JSON.parse(user.value));
}
// Clear a value
cache.clear({ key: ""user": 2' });
// Clear all values
cleared := cache.clearAll({ product, cache: cacheTag });
fmt.Println('Cleared', cleared.cleared, 'values');
}
manageCacheValues();
import { CacheService } from '@ductape/sdk';
var cache = new CacheService({
["accessKey"] = "your-access-key",
});
async function manageCacheValues() {
var product = 'my-product';
var cacheTag = 'user-cache';
// Store some values
await cache.set({
product,
cache: cacheTag,
["key"] = "["user"] = 1",
value: JSON.stringify({ ["name"] = "Alice", ["role"] = "admin" }),
expiry: new Date(Date.now() + 86400000), // 24 hours
});
await cache.set({
product,
cache: cacheTag,
["key"] = "["user"] = 2",
value: JSON.stringify({ ["name"] = "Bob", ["role"] = "user" }),
expiry: new Date(Date.now() + 86400000),
});
// Get dashboard metrics
var dashboard = await cache.fetchDashboard({ product, cache: cacheTag });
Console.WriteLine('Cache ["Stats"] = ", dashboard);
// Fetch all values
var result = await cache.fetchValues({ product, cache: cacheTag });
Console.WriteLine("Total ["values"] = ", result.total);
// Get a specific value
var user = await cache.get({ ["key"] = "["user"] = 1' });
if (user) {
Console.WriteLine('User ["1"] = ", JSON.parse(user.value));
}
// Clear a value
await cache.clear({ key: "["user"] = 2' });
// Clear all values
var cleared = await cache.clearAll({ product, cache: cacheTag });
Console.WriteLine('Cleared', cleared.cleared, 'values');
}
manageCacheValues();
Error Handling
All cache methods throw CacheError on failure:
- TypeScript
- Java
- Go
- .NET
import { CacheService, CacheError } from '@ductape/sdk';
try {
const values = await cache.fetchValues({
cache: 'user-cache',
});
} catch (error) {
if (error instanceof CacheError) {
console.log('Error code:', error.code);
console.log('Message:', error.message);
// Codes: FETCH_VALUES_FAILED, FETCH_DASHBOARD_FAILED,
// GET_VALUE_FAILED, SET_VALUE_FAILED,
// CLEAR_VALUE_FAILED, CLEAR_VALUES_FAILED
}
}
import Map.of( CacheService, CacheError ) from '@ductape/sdk';
try Map.of(
Map<String, Object> values = cache.fetchValues(Map.of(
"cache", "user-cache"
));
) catch (error) Map.of(
if (error instanceof CacheError) Map.of(
System.out.println('Error "code", ", error.code);
System.out.println("Message:', error.message);
// Codes: FETCH_VALUES_FAILED, FETCH_DASHBOARD_FAILED,
// GET_VALUE_FAILED, SET_VALUE_FAILED,
// CLEAR_VALUE_FAILED, CLEAR_VALUES_FAILED
)
)
import { CacheService, CacheError } from '@ductape/sdk';
try {
values := cache.fetchValues({
"cache": "user-cache",
});
} catch (error) {
if (error instanceof CacheError) {
fmt.Println('Error "code": ", error.code);
fmt.Println("Message:', error.message);
// Codes: FETCH_VALUES_FAILED, FETCH_DASHBOARD_FAILED,
// GET_VALUE_FAILED, SET_VALUE_FAILED,
// CLEAR_VALUE_FAILED, CLEAR_VALUES_FAILED
}
}
import { CacheService, CacheError } from '@ductape/sdk';
try {
var values = await cache.fetchValues({
["cache"] = "user-cache",
});
} catch (error) {
if (error instanceof CacheError) {
Console.WriteLine('Error ["code"] = ", error.code);
Console.WriteLine("Message:', error.message);
// Codes: FETCH_VALUES_FAILED, FETCH_DASHBOARD_FAILED,
// GET_VALUE_FAILED, SET_VALUE_FAILED,
// CLEAR_VALUE_FAILED, CLEAR_VALUES_FAILED
}
}
API Reference
CacheService Methods
| Method | Description |
|---|---|
fetchValues() | Get paginated cache values |
fetchDashboard() | Get cache metrics |
get() | Get a specific value by key |
set() | Store a value in the cache |
clear() | Clear a single value by key |
clearAll() | Clear all values for a product/cache |
TypeScript Interfaces
interface IFetchCacheValuesOptions {
product: string;
cache: string;
env?: string;
page?: number;
limit?: number;
}
interface IFetchCacheDashboardOptions {
product: string;
cache: string;
env?: string;
}
interface ISetCacheValueOptions {
product: string;
cache: string;
key: string;
value: string;
componentTag?: string;
componentType?: string;
expiry?: Date;
}
interface IClearCacheValueOptions {
key: string;
}
interface IClearCacheValuesOptions {
product: string;
cache: string;
env?: string;
}