# Store data in the browser with CACHE, PREF and COOKIES

> Keep values between visits with CACHE (expiring), PREF (user preferences) and COOKIES.

Framework: jcomponent20
Product: jComponent 20 (jcomponent@20)
Language: en
Content-SHA256: bdb8f20a9c8cdbd741b0e9c31c4c330560cfbbead4075c9e1a7b00df8746650a
Snapshot: c0700e321579725c5cf4f5ace1ad7d2866d1b4f93c24a63c0b41b6b7998e976c
Sources: jcomponent20@3fedd009a277f50fddc608b97afbec553b67c51b
Review: not-recorded

Three helpers store data in the browser:

- `CACHE(key, value, expiration)` stores any value for a period (`'10 minutes'`, `'1 day'`) and `CACHE(key)` reads it, giving `null` once it expired. The data lives in the browser storage, so it survives a reload.
- `PREF.set(name, value)` stores a user preference for a year; read it back as `PREF.name`.
- `COOKIES.set(name, value, expiration)`, `COOKIES.get(name)` and `COOKIES.rem(name)` work with real cookies, which the server receives on every request.

```html public/index.html
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8" />
	<title>jComponent</title>
	<link rel="stylesheet" href="//cdn.componentator.com/spa.min@20.css" />
	<script src="//cdn.componentator.com/spa.min@20.js"></script>
</head>
<body>
	<p class="theme"></p>

	<script>

		CACHE('profile', { name: 'Peter', age: 42 }, '1 hour');
		CACHE('short', 'value', '1 second');

		PREF.set('theme', 'dark');
		$('.theme').text('Theme: ' + PREF.theme);

		COOKIES.set('visited', 'yes', '1 day');

	</script>

</body>
</html>
```
