-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1668051
commit 252d99f
Showing
4 changed files
with
124 additions
and
1 deletion.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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(); | ||
}) | ||
}) | ||
}, | ||
} | ||
}); |
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,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(); | ||
}); | ||
}); |