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
Edward de Groot
committed
Mar 21, 2012
1 parent
f9592d0
commit a3b8aa9
Showing
10 changed files
with
105 additions
and
21 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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
pantry = require '../src/pantry' | ||
MemcachedStorage = require '../src/pantry-memcached' | ||
|
||
delay = (ms, func) -> setTimeout func, ms | ||
|
||
test = (id, sleep)-> | ||
pantry.fetch "http://app.canada.com/southparc/query.svc/content/#{id}?format=json", (error, item) -> | ||
delay sleep, -> test id, sleep | ||
|
||
pantry.configure { shelfLife: 10, maxLife: 30, caseSensitive: false, verbosity: 'DEBUG'} | ||
pantry.storage = new MemcachedStorage 'localhost:11211', {}, 'DEBUG' | ||
|
||
pantry.fetch "http://app.canada.com/southparc/query.svc/relatedcontent/764023?format=json", (error, list) -> | ||
max = if list.length > 30 then 30 else list.length | ||
for x in [0...max] | ||
do (x) -> | ||
sleep = (x + 1) * 500 | ||
delay sleep, -> test list[x].ID, sleep |
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
Log = require 'coloured-log' | ||
Memcached = require 'memcached' | ||
|
||
module.exports = class MemcachedStorage | ||
constructor: (servers = "localhost:11211", options = {}, verbosity = 'DEBUG') -> | ||
# configure the log | ||
@log = new Log(verbosity) | ||
|
||
# connect to redis server | ||
@client = new Memcached(servers, options) | ||
|
||
@client.on 'issue', (details) -> | ||
@log.warning details | ||
|
||
@client.on 'failure', (details) -> | ||
@log.error details | ||
|
||
@client.on 'reconnecting', (details) -> | ||
@log.notice details | ||
|
||
@client.on 'reconnected', (details) -> | ||
@log.info details | ||
|
||
@client.on 'remove', (details) -> | ||
@log.notice details | ||
|
||
@log.notice "New memcached storage created" | ||
@log.info "Servers: #{servers}" | ||
|
||
# retrieve a specific resource | ||
get: (key, callback) -> | ||
@client.get key, (err, results) -> | ||
if err then @log.error err | ||
callback err, if err then null else JSON.parse(results) | ||
|
||
# save a specific resource | ||
put: (resource, callback) -> | ||
@client.set resource.options.key, JSON.stringify(resource), resource.options.maxLife, (err, results) => | ||
if err then @log.error err | ||
callback(err, results) if callback? | ||
|
||
#allow chaining, mostly for testing | ||
return @ |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
should = require 'should' | ||
|
||
Storage = require '../src/pantry-memcached' | ||
MockResource = require '../mocks/resource-mock' | ||
|
||
describe 'pantry-memcached', -> | ||
describe 'get/put', -> | ||
describe 'when adding an item to storage', -> | ||
storage = new Storage | ||
resource = new MockResource 'fresh', "Hello World #{new Date()}" | ||
it 'should not return an error', (done) -> | ||
storage.put resource, (err, results) -> | ||
done err | ||
it 'should be retrievable', (done) -> | ||
storage.get resource.options.key, (err, item) -> | ||
item.options.should.have.property 'key', resource.options.key | ||
item.should.have.property 'results', resource.results | ||
done err |
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