-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
π feat: lets go to jsr [SMALL BREAKING: READ COMMIT MSG]
β feat: use node redis instead of denodrivers redis
- Loading branch information
1 parent
9ca7082
commit 47677d3
Showing
9 changed files
with
109 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,17 @@ | ||
import { ICacheAdapter } from './adapter.ts' | ||
// Not in deps.ts to allow optional dep loading | ||
import { | ||
connect, | ||
Redis, | ||
RedisConnectOptions, | ||
RedisValue | ||
} from 'https://deno.land/x/[email protected]/mod.ts' | ||
import { createClient, RedisClientOptions } from 'npm:[email protected]' | ||
|
||
/** Redis Cache Adapter for using Redis as a cache-provider. */ | ||
export class RedisCacheAdapter implements ICacheAdapter { | ||
_redis: Promise<Redis> | ||
redis?: Redis | ||
_redis: Promise<ReturnType<typeof createClient>> | ||
redis?: ReturnType<typeof createClient> | ||
ready: boolean = false | ||
readonly _expireIntervalTimer: number = 5000 | ||
private _expireInterval?: number | ||
|
||
constructor(options: RedisConnectOptions) { | ||
this._redis = connect(options) | ||
constructor(options: RedisClientOptions) { | ||
this._redis = createClient(options).connect() | ||
this._redis.then( | ||
(redis) => { | ||
this.redis = redis | ||
|
@@ -31,17 +26,17 @@ export class RedisCacheAdapter implements ICacheAdapter { | |
|
||
private _startExpireInterval(): void { | ||
this._expireInterval = setInterval(() => { | ||
this.redis?.scan(0, { pattern: '*:expires' }).then(([_, names]) => { | ||
this.redis?.scan(0, { MATCH: '*:expires' }).then(({ keys: names }) => { | ||
for (const name of names) { | ||
this.redis?.hvals(name).then((vals) => { | ||
this.redis?.hVals(name).then((vals) => { | ||
for (const val of vals) { | ||
const expireVal: { | ||
name: string | ||
key: string | ||
at: number | ||
} = JSON.parse(val) | ||
const expired = new Date().getTime() > expireVal.at | ||
if (expired) this.redis?.hdel(expireVal.name, expireVal.key) | ||
if (expired) this.redis?.hDel(expireVal.name, expireVal.key) | ||
} | ||
}) | ||
} | ||
|
@@ -55,7 +50,7 @@ export class RedisCacheAdapter implements ICacheAdapter { | |
|
||
async get<T>(cacheName: string, key: string): Promise<T | undefined> { | ||
await this._checkReady() | ||
const cache = await this.redis?.hget(cacheName, key) | ||
const cache = await this.redis?.hGet(cacheName, key) | ||
if (cache === undefined) return | ||
try { | ||
return JSON.parse(cache) as T | ||
|
@@ -71,15 +66,13 @@ export class RedisCacheAdapter implements ICacheAdapter { | |
expire?: number | ||
): Promise<void> { | ||
await this._checkReady() | ||
await this.redis?.hset( | ||
await this.redis?.hSet( | ||
cacheName, | ||
key, | ||
typeof value === 'object' | ||
? JSON.stringify(value) | ||
: (value as unknown as RedisValue) | ||
typeof value === 'object' ? JSON.stringify(value) : (value as any) | ||
) | ||
if (expire !== undefined) { | ||
await this.redis?.hset( | ||
await this.redis?.hSet( | ||
`${cacheName}:expires`, | ||
key, | ||
JSON.stringify({ | ||
|
@@ -93,18 +86,18 @@ export class RedisCacheAdapter implements ICacheAdapter { | |
|
||
async delete(cacheName: string, ...keys: string[]): Promise<boolean> { | ||
await this._checkReady() | ||
return ((await this.redis?.hdel(cacheName, ...keys)) ?? 0) === keys.length | ||
return ((await this.redis?.hDel(cacheName, keys)) ?? 0) === keys.length | ||
} | ||
|
||
async array<T>(cacheName: string): Promise<T[] | undefined> { | ||
await this._checkReady() | ||
const data = await this.redis?.hvals(cacheName) | ||
const data = await this.redis?.hVals(cacheName) | ||
return data?.map((e: string) => JSON.parse(e)) | ||
} | ||
|
||
async keys(cacheName: string): Promise<string[] | undefined> { | ||
await this._checkReady() | ||
return this.redis?.hkeys(cacheName) | ||
return this.redis?.hKeys(cacheName) | ||
} | ||
|
||
async deleteCache(cacheName: string): Promise<boolean> { | ||
|
@@ -114,6 +107,6 @@ export class RedisCacheAdapter implements ICacheAdapter { | |
|
||
async size(cacheName: string): Promise<number | undefined> { | ||
await this._checkReady() | ||
return this.redis?.hlen(cacheName) | ||
return this.redis?.hLen(cacheName) | ||
} | ||
} |
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,8 +1,19 @@ | ||
import { Mixin } from '../../deps.ts' | ||
import type { GuildNewsChannelPayload } from '../types/channel.ts' | ||
import type { Client } from '../client/mod.ts' | ||
import type { Guild } from './guild.ts' | ||
import { GuildTextBasedChannel } from './guildTextChannel.ts' | ||
import { GuildThreadAvailableChannel } from './guildThreadAvailableChannel.ts' | ||
|
||
export class NewsChannel extends Mixin( | ||
GuildTextBasedChannel, | ||
GuildThreadAvailableChannel | ||
) {} | ||
const NewsChannelSuper: (abstract new ( | ||
client: Client, | ||
data: GuildNewsChannelPayload, | ||
guild: Guild | ||
) => GuildTextBasedChannel & GuildThreadAvailableChannel) & | ||
Pick<typeof GuildTextBasedChannel, keyof typeof GuildTextBasedChannel> & | ||
Pick< | ||
typeof GuildThreadAvailableChannel, | ||
keyof typeof GuildThreadAvailableChannel | ||
> = Mixin(GuildTextBasedChannel, GuildThreadAvailableChannel) | ||
|
||
export class NewsChannel extends NewsChannelSuper {} |
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