Skip to content

setCache

Set an item in localStorage with optional expiration.

Usage

js
import { setCache } from 'js-cool'

Signature

typescript
function setCache(key: string, value: any, seconds?: number): void

Parameters

ParameterTypeDescription
keystringThe key to set
valueanyThe value to store
secondsnumberExpiration time in seconds (optional)

Examples

js
// Set without expiration
setCache('user', { name: 'John' })

// Set with expiration (1 hour)
setCache('token', 'abc123', 3600)

// Set with expiration (24 hours)
setCache('settings', { theme: 'dark' }, 86400)

Notes

  • Value is serialized as JSON
  • Expiration time is stored alongside the value
  • Overwrites existing values

Released under the MIT License.