refactor cache

This commit is contained in:
jysperm
2014-11-01 16:34:50 +08:00
parent ed17eeb232
commit fffcb94373
4 changed files with 116 additions and 47 deletions

View File

@@ -65,6 +65,12 @@ do ->
fs.writeFileSync session_key_path, crypto.randomBytes(48).toString('hex')
fs.chmodSync session_key_path, 0o750
app.redis = redis.createClient 6379, '127.0.0.1',
auth_pass: config.redis.password
app.mailer = nodemailer.createTransport config.email.account
app.express = express()
app.config = config
app.db = require './core/db'
app.utils = require './core/utils'
@@ -88,12 +94,6 @@ app.billing = require './core/billing'
app.middleware = require './core/middleware'
app.notification = require './core/notification'
app.redis = redis.createClient 6379, '127.0.0.1',
auth_pass: config.redis.password
app.mailer = nodemailer.createTransport config.email.account
app.express = express()
unless process.env.NODE_ENV == 'test'
app.express.use morgan 'dev'

View File

@@ -1,4 +1,5 @@
stringify = require 'json-stable-stringify'
getParameterNames = require 'get-parameter-names'
CounterCache = require 'counter-cache'
_ = require 'underscore'
@@ -8,57 +9,50 @@ config = require '../config'
exports.counter = new CounterCache()
exports.hashKey = (key, param) ->
return "#{config.redis.prefix}:#{key}" + stringify(param)
exports.hashKey = (key) ->
if _.isString key
return "#{config.redis.prefix}:" + key
else
return "#{config.redis.prefix}:" + stringify key
# @param [options] {param, command, is_json}
# @param setter(callback(value), param)
# @param key: string|object
# @param setter(COMMAND(value, command_params...), key)
# @param callback(value)
exports.try = (key, options, setter, callback) ->
unless callback
callback = setter
setter = options
options = {}
options.param ?= {}
options.command ?= exports.SET()
key = exports.hashKey key, options.param
if _.isEmpty options.param
original_setter = setter
setter = (param, callback) ->
original_setter callback
exports.try = (key, setter, callback) ->
original_key = key
key = exports.hashKey key
redis.get key, (err, value) ->
if value != undefined and value != null
if options.is_json
value = JSON.parse value
callback value
try
callback JSON.parse value
catch e
callback value
else
setter options.param, (value) ->
if options.is_json
setter (value, command_params...) ->
original_value = value
if _.isObject value
value = JSON.stringify value
options.command key, value, ->
callback value
command = _.first getParameterNames setter
command = exports[command.toUpperCase()]
exports.delete = (key, param, callback) ->
unless callback
callback = param
param = {}
params = [key, value].concat command_params
params.push ->
callback original_value
key = exports.hashKey key, param
command.apply @, params
redis.del key, ->
, original_key
exports.delete = (key, callback) ->
redis.del exports.hashKey(key), ->
callback()
exports.SET = ->
return (key, value, callback) ->
redis.set key, value, callback
exports.SET = (key, value, callback) ->
redis.set key, value, callback
exports.SETEX = (seconds) ->
return (key, value, callback) ->
redis.setex key, seconds, value, callback
exports.SETEX = (key, value, seconds, callback) ->
redis.setex key, seconds, value, callback

View File

@@ -1,2 +1,76 @@
describe 'cache', ->
it 'pending'
cache = null
redis = null
before ->
{cache, redis} = app
describe 'hashKey', ->
it 'should success', ->
cache.hashKey('cache_key').should.be.equal 'RP:cache_key'
cache.hashKey({param: 'value'}).should.equal 'RP:{"param":"value"}'
cache.hashKey({a: 'b', c: 'd', e: 2}).should.equal 'RP:{"a":"b","c":"d","e":2}'
cache.hashKey({e: 2, a: 'b', c: 'd'}).should.equal 'RP:{"a":"b","c":"d","e":2}'
describe 'try', ->
it 'should success when cache not exist', (done) ->
cache.try 'test_key', (SET, key) ->
key.should.be.equal 'test_key'
SET 'test_key_value'
, (value) ->
value.should.be.equal 'test_key_value'
redis.get 'RP:test_key', (err, value) ->
value.should.be.equal 'test_key_value'
done()
it 'should success when cache exist', (done) ->
cache.try 'test_key', ->
throw new Error 'should not be called'
, (value) ->
value.should.be.equal 'test_key_value'
done()
it 'should success with param', (done) ->
cache.try
key: 'test2'
object_id: 10
, (SET, key) ->
key.object_id.should.be.equal 10
SET 100
, (value) ->
value.should.be.equal 100
done()
it 'should success with JSON and SETEX', (done) ->
cache.try 'test_key3', (SETEX) ->
SETEX
value_of: 'test_key3'
, 60
, (value) ->
value.value_of.should.be.equal 'test_key3'
redis.ttl 'RP:test_key3', (err, seconds) ->
seconds.should.above 0
cache.delete 'test_key3', done
describe 'delete', ->
it 'should success', (done) ->
cache.delete 'test_key', ->
redis.get 'RP:test_key', (err, value) ->
expect(value).to.not.exist
done()
it 'should success with param', (done) ->
cache.delete
key: 'test2'
object_id: 10
, ->
redis.get
key: 'test2'
object_id: 10
, (err, value) ->
expect(value).to.not.exist
done()

View File

@@ -58,7 +58,8 @@
"request": "^2.45.0",
"tmp": "^0.0.24",
"underscore": "^1.6.0",
"semver": "^4.1.0"
"semver": "^4.1.0",
"get-parameter-names": "^0.2.0"
},
"devDependencies": {
"mocha": "^2.0.0",