-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added redis caching to posts, tags and artists
- Loading branch information
Showing
14 changed files
with
231 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,27 @@ | ||
#!/bin/bash | ||
|
||
ENV_LOCAL_FILE_PATH="../.env.local" | ||
ENV_LOCAL_FILE_PATH_DOCKER="../../.env.local" | ||
|
||
echo "Reading .env.local variables into script environment" | ||
export $(egrep -v '^#' $ENV_LOCAL_FILE_PATH | xargs) | ||
|
||
cd ../docker/postgres | ||
echo "Composing dexbooru database container from Docker Postgres image" | ||
docker-compose --env-file "../../.env.local" up -d | ||
docker-compose --env-file $ENV_LOCAL_FILE_PATH_DOCKER up -d | ||
|
||
cd ../redis | ||
echo "Composing dexbooru redis container from Docker Redis image" | ||
docker-compose --env-file "../../.env.local" up -d | ||
docker-compose --env-file $ENV_LOCAL_FILE_PATH_DOCKER up -d | ||
|
||
echo "Configuring permissions for the user called $DB_USER on the database called $DB_NAME" | ||
docker exec -it dexbooru-postgres psql -U ${DB_USER} -d ${DB_NAME} -c "ALTER USER ${DB_USER} WITH SUPERUSER;" | ||
docker exec -it dexbooru-postgres psql -U ${DB_USER} -d ${DB_NAME} -c "GRANT ALL PRIVILEGES ON DATABASE ${DB_NAME} TO ${DB_USER};" | ||
docker exec -it dexbooru-postgres psql -U ${DB_USER} -d ${DB_NAME} -c "GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA ${DB_SCHEMA} TO ${DB_USER};" | ||
|
||
cd ../../ | ||
echo "Migrating schemas and Seeding the database with mock data" | ||
yarn dbmigrate:dev | ||
yarn dbreset:dev | ||
|
||
|
||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const REDIS_URL = process.env.DB_REDIS_URL ?? ''; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { createClient } from 'redis'; | ||
import { REDIS_URL } from '../constants/database'; | ||
|
||
type RedisClientSingleton = Awaited<ReturnType<typeof redisClientSingleton>>; | ||
|
||
const redisClientSingleton = async () => { | ||
return await createClient({ | ||
url: REDIS_URL | ||
}).connect(); | ||
}; | ||
|
||
const globalForRedis = globalThis as unknown as { | ||
redis: RedisClientSingleton | undefined; | ||
}; | ||
|
||
const redis = globalForRedis.redis ?? (await redisClientSingleton()); | ||
|
||
if (process.env.NODE_ENV !== 'production') { | ||
globalForRedis.redis = redis; | ||
} | ||
|
||
export default redis; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import redis from '$lib/server/db/redis'; | ||
|
||
export async function cacheQuery<ArgsType>(args: ArgsType, query: CallableFunction) { | ||
const stringifiedArgs = JSON.stringify(args); | ||
const queryCacheResult = await redis.get(stringifiedArgs); | ||
if (queryCacheResult) { | ||
return JSON.parse(queryCacheResult); | ||
} | ||
|
||
const queryResults = await query(args); | ||
await redis.set(stringifiedArgs, JSON.stringify(queryResults), { EX: 30 }); | ||
|
||
return queryResults; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.