Skip to content

Commit

Permalink
feat: support memcached
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanicboi authored and Hebilicious committed Jul 2, 2023
1 parent 1668051 commit 252d99f
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 1 deletion.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@
"unstorage": "pnpm jiti src/cli"
},
"dependencies": {
"@types/memcached": "^2.2.7",
"anymatch": "^3.1.3",
"chokidar": "^3.5.3",
"destr": "^2.0.0",
"h3": "^1.7.0",
"ioredis": "^5.3.2",
"listhen": "^1.0.4",
"lru-cache": "^10.0.0",
"memcached": "^2.2.2",
"mri": "^1.2.0",
"node-fetch-native": "^1.2.0",
"ofetch": "^1.1.1",
Expand Down
45 changes: 44 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 55 additions & 0 deletions src/drivers/memcached.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { defineDriver } from "./utils";
import Memcached from 'memcached';

export interface MemcachedOpts {
serverLocation: Memcached.Location;
connectionOptions: Memcached.options;
}


export default defineDriver<MemcachedOpts>((_opts : MemcachedOpts) => {
//@ts-ignore-next-line
const memcached = new Memcached(_opts.serverLocation || 'localhost:11211', _opts.connectionOptions || {});
return {
async getItem(key) {
return await new Promise((resolve, reject) => {
memcached.get(key, (err, data) => {
if (err) reject(err);
resolve(data);
})
});
},


async hasItem(key: string) {
return await new Promise((resolve, reject) => {
memcached.get(key, (err, data) => {
if (err) reject(err);
resolve(data || false);
})
});
},


async setItem(key: string, value: string, lifetime: number | undefined) {
if (lifetime === undefined) throw new Error('Lifetime must be a number when using memcached!');
return await new Promise((resolve, reject) => {
memcached.set(key, value, lifetime, (err, result) => {
if (err) reject(err);
if (!result) reject('Failed');
resolve();
});
});
},

async removeItem(key) {
return await new Promise((resolve, reject) => {
memcached.del(key, (err, result) => {
if (err) reject(err);
if (!result) reject('Failed');
resolve();
})
})
},
}
});
23 changes: 23 additions & 0 deletions test/drivers/memcached.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { describe, it, expect, vi, vitest, beforeEach, afterEach } from 'vitest'
import driver from '../../src/drivers/memcached'
import { createStorage } from '../../src'
import { testDriver } from './utils'

describe('Memcached storage', () => {
const storage = driver({
serverLocation: 'localhost:11211',
connectionOptions: {}
});

it('driver: basic',async () => {
// @ts-ignore
await storage.setItem('basic', 'foo', 0);
let got = await storage.getItem('basic');
const has = await storage.hasItem('basic');
expect(got).toBe('foo');
expect(has).toBeTruthy();
await storage.removeItem('basic');
got = await storage.getItem('basic');
expect(got).toBeUndefined();
});
});

0 comments on commit 252d99f

Please sign in to comment.