Skip to content

String Operations

DBX provides a complete set of string operations through its REST API. These operations allow you to store, retrieve, and manage string values with optional TTL (Time To Live) support.

Overview

String operations are the most basic data type in DBX, supporting simple key-value storage with additional features like expiration and existence checks.

Endpoints

GET /redis/string/{key}

Retrieve a string value by key.

Parameters:
  • key (path) - The key to retrieve
Response:
{
  "value": "Hello, World!",
  "ttl": 3600
}
Example:
curl http://localhost:8080/redis/string/my-key
Error Responses:
  • 404 Not Found - Key doesn't exist
  • 400 Bad Request - Invalid key format

POST /redis/string/{key}

Create or update a string value.

Parameters:
  • key (path) - The key to store
  • value (body) - The string value to store
  • ttl (optional, body) - Time to live in seconds
Request Body:
{
  "value": "Hello, World!",
  "ttl": 3600
}
Response:
{
  "success": true,
  "key": "my-key"
}
Example:
curl -X POST http://localhost:8080/redis/string/my-key \
     -H "Content-Type: application/json" \
     -d '{"value": "Hello, World!", "ttl": 3600}'

DELETE /redis/string/{key}

Delete a string value by key.

Parameters:
  • key (path) - The key to delete
Response:
{
  "success": true,
  "deleted": true
}
Example:
curl -X DELETE http://localhost:8080/redis/string/my-key

GET /redis/string/{key}/exists

Check if a key exists.

Parameters:
  • key (path) - The key to check
Response:
{
  "exists": true
}
Example:
curl http://localhost:8080/redis/string/my-key/exists

GET /redis/string/{key}/ttl

Get the remaining TTL for a key.

Parameters:
  • key (path) - The key to check
Response:
{
  "ttl": 1800
}
Example:
curl http://localhost:8080/redis/string/my-key/ttl

POST /redis/string/{key}/expire

Set or update the TTL for a key.

Parameters:
  • key (path) - The key to update
  • ttl (body) - New TTL in seconds
Request Body:
{
  "ttl": 7200
}
Response:
{
  "success": true,
  "ttl": 7200
}
Example:
curl -X POST http://localhost:8080/redis/string/my-key/expire \
     -H "Content-Type: application/json" \
     -d '{"ttl": 7200}'

Batch Operations

POST /redis/string/batch

Perform multiple string operations in a single request.

Request Body:
{
  "operations": [
    {
      "type": "set",
      "key": "key1",
      "value": "value1",
      "ttl": 3600
    },
    {
      "type": "get",
      "key": "key2"
    },
    {
      "type": "delete",
      "key": "key3"
    }
  ]
}
Response:
{
  "results": [
    {
      "type": "set",
      "success": true,
      "key": "key1"
    },
    {
      "type": "get",
      "success": true,
      "value": "value2"
    },
    {
      "type": "delete",
      "success": true,
      "deleted": true
    }
  ]
}

Use Cases

1. Simple Caching

# Store a cached value
curl -X POST http://localhost:8080/redis/string/cache:user:123 \
     -H "Content-Type: application/json" \
     -d '{"value": "{\"name\":\"John\",\"email\":\"john@example.com\"}", "ttl": 300}'
 
# Retrieve cached value
curl http://localhost:8080/redis/string/cache:user:123

2. Session Storage

# Store session data
curl -X POST http://localhost:8080/redis/string/session:abc123 \
     -H "Content-Type: application/json" \
     -d '{"value": "{\"user_id\":123,\"permissions\":[\"read\",\"write\"]}", "ttl": 3600}'
 
# Check session exists
curl http://localhost:8080/redis/string/session:abc123/exists

3. Rate Limiting

# Track API calls
curl -X POST http://localhost:8080/redis/string/rate:ip:192.168.1.1 \
     -H "Content-Type: application/json" \
     -d '{"value": "10", "ttl": 60}'
 
# Check remaining calls
curl http://localhost:8080/redis/string/rate:ip:192.168.1.1

Error Handling

All endpoints return consistent error responses:

{
  "error": "not_found",
  "message": "Key 'my-key' not found",
  "status": 404
}

Common error types:

  • not_found - Key doesn't exist
  • invalid_key - Key format is invalid
  • invalid_value - Value format is invalid
  • invalid_ttl - TTL value is invalid
  • server_error - Internal server error

Performance Considerations

  1. Key Naming - Use descriptive, hierarchical keys (e.g., user:123:profile)
  2. TTL Management - Set appropriate TTL values to prevent memory bloat
  3. Batch Operations - Use batch endpoints for multiple operations
  4. Connection Pooling - Reuse HTTP connections for better performance

Rate Limits

String operations are subject to the same rate limiting as other API endpoints:

  • Default: 100 requests per minute per IP
  • Configurable via environment variables