Skip to content

Working with Cache

cooffeeRequired edited this page May 9, 2025 · 4 revisions

Working with Cache πŸš€

This guide explains how to work with JSON caching in skJson.

Cache Basics πŸ“š

Creating Virtual Storage

# Create a virtual storage (in-memory)
create json virtual storage named "my-virtual-storage"

Checking Cache Status

# Check if storage is cached
if json storage with id "my-storage" is cached:
    send "Storage is cached!"

# Check if storage is being watched
if json storage with id "my-storage" is listened:
    send "Storage is being watched!"

Cache Operations πŸ”§

Accessing Cached Data

# Get cached storage
set {_data} to json storage of id "my-storage"

# Get all cached storages
set {_all_storages::*} to all json storages

Saving Cache

# Save specific storage
save json storage id "my-storage"

# Save all storages
save all json storages

Cache Management πŸ“Š

Mapping JSON to Variables

# Map JSON to Skript variables
map {_json} to {_mapped::*}

# Async mapping
async map {_json} to {_mapped::*}

Getting JSON Size

# Get size of JSON object/array
set {_size} to json size of {_json}

Cache Invalidation

# Check if cache is valid
if json storage with id "my-storage" is cached:
    set {_data} to json storage of id "my-storage"
else:
    # Reload from file
    set {_data} to json from file "data.json"
    set json storage of id "my-storage" to {_data}

Best Practices πŸ’‘

  1. Use meaningful storage IDs
  2. Implement proper cache invalidation
  3. Save cache regularly
  4. Monitor cache size
  5. Use virtual storage for temporary data
  6. Handle cache errors appropriately
  7. Implement cache cleanup strategies

Examples πŸ“

Player Data Cache

# Create virtual storage for player data
create json virtual storage named "player-%player's uuid%"

# Store player data
set {_data} to json from "{'name': '%player%', 'lastLogin': '%now%'}"
set json storage of id "player-%player's uuid%" to {_data}

# Access cached data
set {_player_data} to json storage of id "player-%player's uuid%"

Configuration Cache

# Create config cache
create json virtual storage named "config"

# Load config from file
set {_config} to json from file "config.json"
set json storage of id "config" to {_config}

# Watch for changes
on json watcher file change:
    if event-uuid is "config":
        set json storage of id "config" to event-json

Cache Mapping Example

# Create and populate cache
set {_json} to json from "{'users': [{'name': 'John'}, {'name': 'Jane'}]}"
set json storage of id "users" to {_json}

# Map to variables
map json storage of id "users" to {_users::*}

# Access mapped data
loop {_users::*}:
    send "User: %loop-value%"

Common Issues and Solutions πŸ”

Cache Invalidation

# Check if cache is valid
if json storage with id "my-storage" is cached:
    set {_data} to json storage of id "my-storage"
else:
    # Reload from file
    set {_data} to json from file "data.json"
    set json storage of id "my-storage" to {_data}

Memory Management

# Clear unused cache
if json size of {_json} > 1000:
    save json storage id "my-storage"
    unbind json storage id "my-storage"

Cache Synchronization

# Ensure cache is up to date
on json watcher file change:
    if event-uuid is "my-storage":
        set json storage of id "my-storage" to event-json
        save json storage id "my-storage"

Next Steps πŸ‘£

Note

For more information about Markdown formatting and alerts, see GitHub's discussion on Markdown alerts.