2014-07-24 08:24:08 -04:00
|
|
|
SystemMessage = require("../../models/SystemMessage").SystemMessage
|
|
|
|
|
|
|
|
module.exports = SystemMessageManager =
|
|
|
|
getMessages: (callback = (error, messages) ->) ->
|
|
|
|
if @_cachedMessages?
|
|
|
|
return callback null, @_cachedMessages
|
|
|
|
else
|
|
|
|
@getMessagesFromDB (error, messages) =>
|
|
|
|
return callback(error) if error?
|
|
|
|
@_cachedMessages = messages
|
|
|
|
return callback null, messages
|
|
|
|
|
|
|
|
getMessagesFromDB: (callback = (error, messages) ->) ->
|
|
|
|
SystemMessage.find {}, callback
|
|
|
|
|
|
|
|
clearMessages: (callback = (error) ->) ->
|
|
|
|
SystemMessage.remove {}, callback
|
|
|
|
|
|
|
|
createMessage: (content, callback = (error) ->) ->
|
|
|
|
message = new SystemMessage { content: content }
|
|
|
|
message.save callback
|
|
|
|
|
|
|
|
clearCache: () ->
|
|
|
|
delete @_cachedMessages
|
|
|
|
|
2017-02-28 13:53:34 -05:00
|
|
|
CACHE_TIMEOUT = 1 * 60 * 1000 # 1 minutes
|
2014-07-24 08:24:08 -04:00
|
|
|
setInterval () ->
|
|
|
|
SystemMessageManager.clearCache()
|
|
|
|
, CACHE_TIMEOUT
|