Skip to content

Commit

Permalink
Merge branch 'main' into timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-R44 committed Oct 29, 2023
2 parents 1c91abc + cc4ff34 commit 5561beb
Show file tree
Hide file tree
Showing 14 changed files with 168 additions and 166 deletions.
68 changes: 68 additions & 0 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: CI

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build:
runs-on: ubuntu-latest

services:
redis:
image: redis:6.2-alpine
ports:
- 6379:6379
redis-insight:
image: redislabs/redisinsight:latest
ports:
- 8001:8001
dynamodb:
image: amazon/dynamodb-local
ports:
- 8000:8000
postgres:
image: postgres:15-alpine
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: postgres
ports:
- 5432:5432
mysql:
image: mysql:8.0
env:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: mysql
ports:
- 3306:3306

steps:
- uses: actions/checkout@v2

- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: '16'

- name: Cache pnpm modules
uses: actions/cache@v2
with:
path: ~/.pnpm-store
key: ${{ runner.os }}-pnpm-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-
- name: Install dependencies
run: pnpm install

- name: Lint
run: pnpm lint

- name: Typecheck
run: pnpm typecheck

- name: Run tests
run: pnpm test
12 changes: 0 additions & 12 deletions drivers/lru.ts

This file was deleted.

4 changes: 2 additions & 2 deletions factories/bentocache_factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export class BentoCacheFactory {
default: 'primary',
ttl: '30s',
stores: {
primary: { driver: memoryDriver({ maxSize: 100 }) },
secondary: { driver: memoryDriver({ maxSize: 100 }) },
primary: { driver: memoryDriver({ maxItems: 100 }) },
secondary: { driver: memoryDriver({ maxItems: 100 }) },
},
...this.#parameters,
})
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@
"typescript": "^5.1.6"
},
"dependencies": {
"@isaacs/ttlcache": "^1.4.1",
"@paralleldrive/cuid2": "^2.2.1",
"@poppinss/hooks": "7.1.1-4",
"@poppinss/utils": "6.5.0-3",
"@sindresorhus/chunkify": "^0.2.0",
"async-mutex": "^0.4.0",
"p-timeout": "^6.1.2",
"quick-lru": "^6.1.1",
"lru-cache": "^10.0.1",
"typescript-log": "^2.0.0",
"uid": "^2.0.2"
},
Expand Down
103 changes: 0 additions & 103 deletions src/drivers/lru.ts

This file was deleted.

30 changes: 17 additions & 13 deletions src/drivers/memory.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
import TTLCache from '@isaacs/ttlcache'
import { LRUCache } from 'lru-cache'

import { BaseDriver } from './base_driver.js'
import type { CacheDriver, MemoryConfig as MemoryConfig } from '../types/main.js'

/**
* A memory caching driver
*
* Will really remove entries as soon as they expire
* ( using a setTimeout ) unlike the Lru driver
*/
export class Memory extends BaseDriver implements CacheDriver {
#cache: TTLCache<string, string>
#cache: LRUCache<string, string>
declare config: MemoryConfig

constructor(config: MemoryConfig & { cacheInstance?: TTLCache<string, string> }) {
constructor(config: MemoryConfig & { cacheInstance?: LRUCache<string, string> }) {
super(config)

if (config.cacheInstance) {
this.#cache = config.cacheInstance
} else {
this.#cache = new TTLCache({ max: config.maxSize, checkAgeOnGet: true })
return
}

this.#cache = new LRUCache({
max: config.maxItems ?? 1000,
maxEntrySize: config.maxEntrySize,
ttlAutopurge: true,
...(config.maxSize
? {
maxSize: config.maxSize,
sizeCalculation: (value) => Buffer.byteLength(value, 'utf-8'),
}
: {}),
})
}

/**
Expand Down Expand Up @@ -81,11 +89,7 @@ export class Memory extends BaseDriver implements CacheDriver {
* Remove all items from the cache
*/
async clear() {
// @ts-expect-error. keys() and entries() values doesn't
// return keys that are stored forever. So we need
// to use this internal `data` property
// See https://github.com/isaacs/ttlcache/issues/27
for (const [key] of this.#cache.data) {
for (const key of this.#cache.keys()) {
if (key.startsWith(this.prefix)) {
this.#cache.delete(key)
}
Expand Down
19 changes: 17 additions & 2 deletions src/types/options/drivers_options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,27 @@ export type DynamoDBConfig = {
*/
export type MemoryConfig = {
/**
* Maximum number of items to store in the cache
* before removing the least recently used items
* Maximum number of items to store in the cache.
*
* Note that fewer items may be stored if you
* are also using `maxSize` and the cache is full.
*
* @default 1000
*/
maxItems?: number

/**
* Maximum size of the cache in bytes.
*/
maxSize?: number

/**
* Maximum size of one entry in bytes.
*
* If an entry is larger than this value,
* it will NOT be stored
*/
maxEntrySize?: number
} & DriverCommonOptions

/**
Expand Down
12 changes: 6 additions & 6 deletions tests/bento_cache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { test } from '@japa/runner'
import EventEmitter from 'node:events'

import { BentoCache } from '../src/bento_cache.js'
import { lruDriver } from '../drivers/lru.js'
import { memoryDriver } from '../drivers/memory.js'
import { hybridDriver } from '../drivers/hybrid.js'
import { REDIS_CREDENTIALS } from '../test_helpers/index.js'
import { redisBusDriver, redisDriver } from '../drivers/redis.js'
Expand All @@ -14,13 +14,13 @@ test.group('Bento Cache', () => {
test('should accept EventEmitter or Emittery', async ({ expectTypeOf }) => {
expectTypeOf(BentoCache).toBeConstructibleWith({
default: 'memory',
stores: { memory: { driver: lruDriver({}) } },
stores: { memory: { driver: memoryDriver({}) } },
emitter: new EventEmitter(),
})

expectTypeOf(BentoCache).toBeConstructibleWith({
default: 'memory',
stores: { memory: { driver: lruDriver({}) } },
stores: { memory: { driver: memoryDriver({}) } },
emitter: new Emittery(),
})
})
Expand Down Expand Up @@ -55,7 +55,7 @@ test.group('Bento Cache', () => {
const bento = new BentoCache({
default: 'memory',
stores: {
memory: { driver: lruDriver({}) },
memory: { driver: memoryDriver({}) },
redis: { driver: redisDriver({ connection: REDIS_CREDENTIALS }) },
},
})
Expand All @@ -74,11 +74,11 @@ test.group('Bento Cache', () => {
const bento = new BentoCache({
default: 'memory',
stores: {
memory: { driver: lruDriver({}) },
memory: { driver: memoryDriver({}) },

hybrid: {
driver: hybridDriver({
local: lruDriver({ maxSize: 1000 }),
local: memoryDriver({ maxItems: 1000 }),
remote: redisDriver({ connection: REDIS_CREDENTIALS }),
bus: redisBusDriver({ connection: REDIS_CREDENTIALS }),
}),
Expand Down
2 changes: 1 addition & 1 deletion tests/bus/bus.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ test.group('Bus synchronization', () => {
test('when a entry is set, other nodes should just logically invalidate the entry, but keep for grace period', async ({
assert,
}) => {
const remoteDriver = new ChaosCache(new Memory({ maxSize: 10, prefix: 'test' }))
const remoteDriver = new ChaosCache(new Memory({ maxItems: 10, prefix: 'test' }))

const [cache1] = new CacheFactory()
.merge({ remoteDriver, gracePeriod: { enabled: true, duration: '12h' } })
Expand Down
4 changes: 2 additions & 2 deletions tests/cache/stampede_protection.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { test } from '@japa/runner'
import { setTimeout } from 'node:timers/promises'

import { MemoryLru } from '../../src/drivers/lru.js'
import { Memory } from '../../src/drivers/memory.js'
import { throwingFactory } from '../../test_helpers/index.js'
import { CacheFactory } from '../../factories/cache_factory.js'

Expand Down Expand Up @@ -37,7 +37,7 @@ test.group('Cache | Stampede protection', () => {
})

test('multiple concurrent calls should ask remote only once', async ({ assert }) => {
class RemoteDriver extends MemoryLru {
class RemoteDriver extends Memory {
askedKeys: string[] = []

get(key: string) {
Expand Down
Loading

0 comments on commit 5561beb

Please sign in to comment.