SHARE
TWEET

memcached set sub-object in node.js

a guest Apr 25th, 2012 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.       // memcached set up
  2.       var nMemcached = require('memcached');
  3.       var memcached = new nMemcached(['192.168.200.3:11211']);
  4.  
  5.       // original data structure
  6.       var room_tokens = {};
  7.  
  8.       // some initial game value
  9.       var appinfo = 'poker'
  10.         , token = 'ABCDEFGHI'
  11.         , slots = 4
  12.         , joining_time = 30000
  13.         , display = 'display_' + token;
  14.  
  15.       // register to room_tokens to find it later & init room properties
  16.       // INTENT TO DO
  17.       room_tokens[token] = {
  18.         slot_available: slots,
  19.         slot_left: slots,
  20.         joining_time_left: joining_time,
  21.         user_list: {},
  22.         game_state: 0//0: selection; 1: game started
  23.       };      
  24.       // MEMCACHED APPROACH
  25.       memcached.set(
  26.         appinfo + '_' + token, {
  27.           slot_available: slots,
  28.           slot_left: slots,
  29.           joining_time_left: joining_time,
  30.           user_list: {},
  31.           game_state: 0//0: selection; 1: game started
  32.         },
  33.         10000,
  34.         function(err, result){
  35.           if(err) {
  36.             console.error(err);
  37.           }
  38.           console.dir(result);
  39.         }
  40.       );
  41.  
  42.       // register display to the room for later fast duplicate name indexing
  43.       // INTENT TO DO
  44.       room_tokens[token]['user_list'][user_name] = display;
  45.       // MEMCACHED APPROACH. This will result the object got wipe out to only user_list sub-object
  46.       memcached.set(
  47.         appinfo + '_' + token, {
  48.           user_list: { ''+user_name: display }
  49.         },
  50.         10000,
  51.         function(err, result){
  52.           if(err) {
  53.             console.error(err);
  54.           }
  55.           console.dir(result);
  56.         }
  57.       );
  58.  
  59.       // note that I could memcached.get() then memcached.set() to do the updating,
  60.       // but have to do it every time
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top