-
Notifications
You must be signed in to change notification settings - Fork 4
Working with Cache
cooffeeRequired edited this page May 9, 2025
·
4 revisions
This guide explains how to work with JSON caching in skJson.
# Create a virtual storage (in-memory)
create json virtual storage named "my-virtual-storage"
# 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!"
# Get cached storage
set {_data} to json storage of id "my-storage"
# Get all cached storages
set {_all_storages::*} to all json storages
# Save specific storage
save json storage id "my-storage"
# Save all storages
save all json storages
# Map JSON to Skript variables
map {_json} to {_mapped::*}
# Async mapping
async map {_json} to {_mapped::*}
# Get size of JSON object/array
set {_size} to json size of {_json}
# 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}
- Use meaningful storage IDs
- Implement proper cache invalidation
- Save cache regularly
- Monitor cache size
- Use virtual storage for temporary data
- Handle cache errors appropriately
- Implement cache cleanup strategies
# 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%"
# 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
# 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%"
# 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}
# Clear unused cache
if json size of {_json} > 1000:
save json storage id "my-storage"
unbind json storage id "my-storage"
# 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"
- Learn about Working with Requests
- See how to Modify JSON
- Return to Working with Files
- Check out Examples for more usage examples
- Learn about Creating JSON
Note
For more information about Markdown formatting and alerts, see GitHub's discussion on Markdown alerts.