Skip to content

Commit 96c6b11

Browse files
committed
thomasgauvin: adapt query caching to mysql
1 parent 847ad47 commit 96c6b11

File tree

1 file changed

+108
-21
lines changed

1 file changed

+108
-21
lines changed

src/content/docs/hyperdrive/configuration/query-caching.mdx

Lines changed: 108 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ sidebar:
55
order: 4
66
---
77

8+
import { TabItem, Tabs } from "~/components";
9+
810
Hyperdrive automatically caches the most popular queries executed against your database, reducing the need to go back to your database (incurring latency and database load) for every query.
911

1012
## What does Hyperdrive cache?
@@ -15,22 +17,47 @@ Besides determining the difference between a `SELECT` and an `INSERT`, Hyperdriv
1517

1618
For example, a read query that populates the front page of a news site would be cached:
1719

18-
```sql
19-
-- Cacheable
20-
SELECT * FROM articles
21-
WHERE DATE(published_time) = CURRENT_DATE()
22-
ORDER BY published_time DESC
23-
LIMIT 50
24-
```
20+
<Tabs>
21+
<TabItem label="PostgreSQL">
22+
```sql
23+
-- Cacheable
24+
SELECT * FROM articles
25+
WHERE DATE(published_time) = CURRENT_DATE()
26+
ORDER BY published_time DESC
27+
LIMIT 50
28+
```
29+
</TabItem>
30+
<TabItem label="MySQL">
31+
```sql
32+
-- Cacheable
33+
SELECT * FROM articles
34+
WHERE DATE(published_time) = CURDATE()
35+
ORDER BY published_time DESC
36+
LIMIT 50
37+
```
38+
</TabItem>
39+
</Tabs>
2540

2641
Mutating queries (including `INSERT`, `UPSERT`, or `CREATE TABLE`) and queries that use [functions designated as `volatile` by PostgreSQL](https://www.postgresql.org/docs/current/xfunc-volatility.html) are not cached:
2742

28-
```sql
29-
-- Not cached
30-
INSERT INTO users(id, name, email) VALUES(555, 'Matt', '[email protected]');
31-
32-
SELECT LASTVAL(), * FROM articles LIMIT 50;
33-
```
43+
<Tabs>
44+
<TabItem label="PostgreSQL">
45+
```sql
46+
-- Not cached
47+
INSERT INTO users(id, name, email) VALUES(555, 'Matt', '[email protected]');
48+
49+
SELECT LASTVAL(), * FROM articles LIMIT 50;
50+
```
51+
</TabItem>
52+
<TabItem label="MySQL">
53+
```sql
54+
-- Not cached
55+
INSERT INTO users(id, name, email) VALUES(555, 'Thomas', '[email protected]');
56+
57+
SELECT LAST_INSERT_ID(), * FROM articles LIMIT 50;
58+
```
59+
</TabItem>
60+
</Tabs>
3461

3562
## Default cache settings
3663

@@ -58,18 +85,78 @@ npx wrangler hyperdrive update my-hyperdrive-id --origin-password my-db-password
5885

5986
You can also configure multiple Hyperdrive connections from a single application: one connection that enables caching for popular queries, and a second connection where you do not want to cache queries, but still benefit from Hyperdrive's latency benefits and connection pooling.
6087

61-
For example, using the [Postgres.js](/hyperdrive/configuration/connect-to-postgres/) driver:
88+
For example, using database drivers:
6289

63-
```ts
64-
const client = new Client({
65-
connectionString: env.HYPERDRIVE.connectionString,
90+
<Tabs>
91+
<TabItem label="PostgreSQL">
92+
<Tabs>
93+
<TabItem label="index.ts">
94+
```ts
95+
const client = postgres(env.HYPERDRIVE.connectionString);
96+
// ...
97+
const clientNoCache = postgres(env.HYPERDRIVE_CACHE_DISABLED.connectionString);
98+
```
99+
</TabItem>
100+
<TabItem label="wrangler.jsonc">
101+
```jsonc
102+
{
103+
// Rest of file
104+
"hyperdrive": [
105+
{
106+
"binding": "HYPERDRIVE",
107+
"id": "<YOUR_HYPERDRIVE_CACHE_ENABLED_CONFIGURATION_ID>"
108+
},
109+
{
110+
"binding": "HYPERDRIVE_CACHE_DISABLED",
111+
"id": "<YOUR_HYPERDRIVE_CACHE_DISABLED_CONFIGURATION_ID>"
112+
}
113+
]
114+
}
115+
```
116+
</TabItem>
117+
</Tabs>
118+
</TabItem>
119+
<TabItem label="MySQL">
120+
<Tabs>
121+
<TabItem label="index.ts">
122+
```ts
123+
const connection = createConnection({
124+
host: env.HYPERDRIVE.host,
125+
user: env.HYPERDRIVE.user,
126+
password: env.HYPERDRIVE.password,
127+
database: env.HYPERDRIVE.database,
128+
port: env.HYPERDRIVE.port
66129
});
67130
// ...
68-
const noCachingClient = new Client({
69-
// This represents a Hyperdrive configuration with the cache disabled
70-
connectionString: env.HYPERDRIVE_CACHE_DISABLED.connectionString,
131+
const connectionNoCache = createConnection({
132+
host: env.HYPERDRIVE_CACHE_DISABLED.host,
133+
user: env.HYPERDRIVE_CACHE_DISABLED.user,
134+
password: env.HYPERDRIVE_CACHE_DISABLED.password,
135+
database: env.HYPERDRIVE_CACHE_DISABLED.database,
136+
port: env.HYPERDRIVE_CACHE_DISABLED.port
71137
});
72-
```
138+
```
139+
</TabItem>
140+
<TabItem label="wrangler.jsonc">
141+
```jsonc
142+
{
143+
// Rest of file
144+
"hyperdrive": [
145+
{
146+
"binding": "HYPERDRIVE",
147+
"id": "<YOUR_HYPERDRIVE_CACHE_ENABLED_CONFIGURATION_ID>"
148+
},
149+
{
150+
"binding": "HYPERDRIVE_CACHE_DISABLED",
151+
"id": "<YOUR_HYPERDRIVE_CACHE_DISABLED_CONFIGURATION_ID>"
152+
}
153+
]
154+
}
155+
```
156+
</TabItem>
157+
</Tabs>
158+
</TabItem>
159+
</Tabs>
73160

74161
## Next steps
75162

0 commit comments

Comments
 (0)