The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. June 25, 2019, 09:16:16 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: cache looping for distributed performance (node.js)  (Read 1400 times)
taky
Rookie
**
Offline Offline

Posts: 13


View Profile
« on: August 06, 2014, 01:02:10 AM »

iced-coffee-script (https://github.com/maxtaco/coffee-script)
"_" is my lodash library with some convenience mixins

object simply keeps a potentially expensive query result in program memory, example at bottom

Code:
# vim: set expandtab tabstop=2 shiftwidth=2 softtabstop=2
_ = require 'taky'
cache = require 'memory-cache'

module.exports = class CacheLoop

  fn: no
  key: 'key'
  prefix: 'cachel'
  interval: '1 min'

  constructor: (@obj) ->
    this[k] = v for k,v of obj

    @fn = @use if @use
    @fn = @refresh if @refresh
    @interval = @every if @every

    if !@fn
      throw new Error 'You must provide a function value for "fn"'

    @_cycle()
    _.every @interval, => @_cycle()

  read: ->
    if hit = cache.get (@prefix or '') + ":#{@key}"
      hit
    else
      null

  val: -> @read()
  value: -> @read()

  _cycle: ->
    if _.type(@fn) is 'function'
      @fn (err,res) =>
        if !err
          cache.put ((@prefix or '') + ":#{@key}"), res
        else

          # todo: use backup storage (redis,memcached)

###
c = new Cacher {
  fn: (cb) ->
    cb new Date().getTime()
  interval: '5 seconds'
}

_.every '1s', -> log c.read()

1406767835607
1406767835607
1406767835607
1406767835607
1406767835607
1406767840608
1406767840608
1406767840608
1406767840608
1406767840608
1406767845609
1406767845609
###
Logged

No links in signatures please
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #1 on: August 26, 2014, 06:32:17 AM »

I've been working with Kaptain Krayola on a pretty big system built on node ... I like it a lot.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!