From eb8224e7b6fbc1f36880bf14c80ee18dcf785c9d Mon Sep 17 00:00:00 2001 From: Aurelie Date: Fri, 14 Jun 2024 16:37:33 +0200 Subject: [PATCH] add redis --- back-end/package-lock.json | 86 ++++++++++++++++++++++++++++++++++++++ back-end/package.json | 1 + back-end/src/cache.ts | 12 ++++++ back-end/src/index.ts | 3 ++ docker-compose.prod.yml | 8 +++- docker-compose.yml | 4 ++ 6 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 back-end/src/cache.ts diff --git a/back-end/package-lock.json b/back-end/package-lock.json index fd5f9ea..8a78b93 100644 --- a/back-end/package-lock.json +++ b/back-end/package-lock.json @@ -17,6 +17,7 @@ "graphql": "^16.8.1", "graphql-scalars": "^1.22.4", "pg": "^8.11.3", + "redis": "^4.6.14", "reflect-metadata": "^0.1.13", "sqlite3": "^5.1.6", "type-graphql": "^2.0.0-beta.3", @@ -1477,6 +1478,59 @@ "resolved": "https://registry.npmjs.org/@protobufjs/utf8/-/utf8-1.1.0.tgz", "integrity": "sha512-Vvn3zZrhQZkkBE8LSuW3em98c0FwgO4nxzv6OdSxPKJIEKY2bGbHn+mhGIPerzI4twdxaP8/0+06HBpwf345Lw==" }, + "node_modules/@redis/bloom": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/@redis/bloom/-/bloom-1.2.0.tgz", + "integrity": "sha512-HG2DFjYKbpNmVXsa0keLHp/3leGJz1mjh09f2RLGGLQZzSHpkmZWuwJbAvo3QcRY8p80m5+ZdXZdYOSBLlp7Cg==", + "peerDependencies": { + "@redis/client": "^1.0.0" + } + }, + "node_modules/@redis/client": { + "version": "1.5.16", + "resolved": "https://registry.npmjs.org/@redis/client/-/client-1.5.16.tgz", + "integrity": "sha512-X1a3xQ5kEMvTib5fBrHKh6Y+pXbeKXqziYuxOUo1ojQNECg4M5Etd1qqyhMap+lFUOAh8S7UYevgJHOm4A+NOg==", + "dependencies": { + "cluster-key-slot": "1.1.2", + "generic-pool": "3.9.0", + "yallist": "4.0.0" + }, + "engines": { + "node": ">=14" + } + }, + "node_modules/@redis/graph": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@redis/graph/-/graph-1.1.1.tgz", + "integrity": "sha512-FEMTcTHZozZciLRl6GiiIB4zGm5z5F3F6a6FZCyrfxdKOhFlGkiAqlexWMBzCi4DcRoyiOsuLfW+cjlGWyExOw==", + "peerDependencies": { + "@redis/client": "^1.0.0" + } + }, + "node_modules/@redis/json": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/@redis/json/-/json-1.0.6.tgz", + "integrity": "sha512-rcZO3bfQbm2zPRpqo82XbW8zg4G/w4W3tI7X8Mqleq9goQjAGLL7q/1n1ZX4dXEAmORVZ4s1+uKLaUOg7LrUhw==", + "peerDependencies": { + "@redis/client": "^1.0.0" + } + }, + "node_modules/@redis/search": { + "version": "1.1.6", + "resolved": "https://registry.npmjs.org/@redis/search/-/search-1.1.6.tgz", + "integrity": "sha512-mZXCxbTYKBQ3M2lZnEddwEAks0Kc7nauire8q20oA0oA/LoA+E/b5Y5KZn232ztPb1FkIGqo12vh3Lf+Vw5iTw==", + "peerDependencies": { + "@redis/client": "^1.0.0" + } + }, + "node_modules/@redis/time-series": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/@redis/time-series/-/time-series-1.0.5.tgz", + "integrity": "sha512-IFjIgTusQym2B5IZJG3XKr5llka7ey84fw/NOYqESP5WUfQs9zz1ww/9+qoz4ka/S6KcGBodzlCeZ5UImKbscg==", + "peerDependencies": { + "@redis/client": "^1.0.0" + } + }, "node_modules/@sinclair/typebox": { "version": "0.27.8", "resolved": "https://registry.npmjs.org/@sinclair/typebox/-/typebox-0.27.8.tgz", @@ -2505,6 +2559,14 @@ "node": ">=12" } }, + "node_modules/cluster-key-slot": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/cluster-key-slot/-/cluster-key-slot-1.1.2.tgz", + "integrity": "sha512-RMr0FhtfXemyinomL4hrWcYJxmX6deFdCxpJzhDttxgO1+bcCnkk+9drydLVDmAMG7NE6aN/fl4F7ucU/90gAA==", + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/co": { "version": "4.6.0", "resolved": "https://registry.npmjs.org/co/-/co-4.6.0.tgz", @@ -3128,6 +3190,14 @@ "node": ">=10" } }, + "node_modules/generic-pool": { + "version": "3.9.0", + "resolved": "https://registry.npmjs.org/generic-pool/-/generic-pool-3.9.0.tgz", + "integrity": "sha512-hymDOu5B53XvN4QT9dBmZxPX4CWhBPPLguTZ9MMFeFa/Kg0xWVfylOVNlJji/E7yTZWFd/q9GO5TxDLq156D7g==", + "engines": { + "node": ">= 4" + } + }, "node_modules/gensync": { "version": "1.0.0-beta.2", "resolved": "https://registry.npmjs.org/gensync/-/gensync-1.0.0-beta.2.tgz", @@ -5505,6 +5575,22 @@ "node": ">=8.10.0" } }, + "node_modules/redis": { + "version": "4.6.14", + "resolved": "https://registry.npmjs.org/redis/-/redis-4.6.14.tgz", + "integrity": "sha512-GrNg/e33HtsQwNXL7kJT+iNFPSwE1IPmd7wzV3j4f2z0EYxZfZE7FVTmUysgAtqQQtg5NXF5SNLR9OdO/UHOfw==", + "workspaces": [ + "./packages/*" + ], + "dependencies": { + "@redis/bloom": "1.2.0", + "@redis/client": "1.5.16", + "@redis/graph": "1.1.1", + "@redis/json": "1.0.6", + "@redis/search": "1.1.6", + "@redis/time-series": "1.0.5" + } + }, "node_modules/reflect-metadata": { "version": "0.1.13", "resolved": "https://registry.npmjs.org/reflect-metadata/-/reflect-metadata-0.1.13.tgz", diff --git a/back-end/package.json b/back-end/package.json index 0499e97..ccd9e99 100644 --- a/back-end/package.json +++ b/back-end/package.json @@ -33,6 +33,7 @@ "graphql": "^16.8.1", "graphql-scalars": "^1.22.4", "pg": "^8.11.3", + "redis": "^4.6.14", "reflect-metadata": "^0.1.13", "sqlite3": "^5.1.6", "type-graphql": "^2.0.0-beta.3", diff --git a/back-end/src/cache.ts b/back-end/src/cache.ts new file mode 100644 index 0000000..01ed318 --- /dev/null +++ b/back-end/src/cache.ts @@ -0,0 +1,12 @@ +import { RedisClientType } from "@redis/client"; +import { createClient } from "redis"; + +let cacheClient: RedisClientType; + +export const getCache = async () => { + if (!cacheClient) { + cacheClient = createClient({ url: process.env.CACHE_URL }); + await cacheClient.connect(); + } + return cacheClient; +}; diff --git a/back-end/src/index.ts b/back-end/src/index.ts index 1189412..9bdddfa 100644 --- a/back-end/src/index.ts +++ b/back-end/src/index.ts @@ -12,6 +12,7 @@ import { TransportationResolver } from "./resolvers/TransportationResolver"; import { RideResolver } from "./resolvers/RideResolver"; import { getDataSource } from "./database"; import { UserResolver } from "./resolvers/UserResolver"; +import { getCache } from "./cache"; export type Context = { req: Request; @@ -48,6 +49,8 @@ const startApolloServer = async () => { await getDataSource(); await Transportation.initializeTransportations(); + await getCache(); + console.log(`🚀 Server ready at: ${url}`); }; diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index 54ebac1..88fb4f2 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -8,6 +8,7 @@ services: environment: - DATABASE_URL=postgres://postgres:password@database:5432/postgres - NODE_ENV=production + - CACHE_URL=redis://cache depends_on: - database expose: @@ -23,7 +24,7 @@ services: expose: - 3000 restart: always - + database: image: postgres:15 volumes: @@ -43,5 +44,8 @@ services: - ./gateway/nginx.conf:/etc/nginx/nginx.conf restart: always + cache: + image: redis + volumes: - database-data: \ No newline at end of file + database-data: diff --git a/docker-compose.yml b/docker-compose.yml index 88b5e89..ccd527a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,6 +6,7 @@ services: environment: - DATABASE_URL=postgres://postgres:password@database:5432/postgres - TEST_DATABASE_URL=postgres://postgres:password@test-database:5432/postgres + - CACHE_URL=redis://cache depends_on: - database ports: @@ -44,5 +45,8 @@ services: environment: - POSTGRES_PASSWORD=password + cache: + image: redis + volumes: database-data: