0

I am trying to write a node app that sits on top of memchached sever. The goal is to share some data among several sites, caching some data in memcache.

All the examples I have found using 'get' from memcache return a promise, then does console.log. However, I need the value for returned to the caller of the function. Hence, I loose scope.

Does anybody of an example of doing get as blocking call?

class TPCacheManager{
    constructor(){
        this.getVal = '';


    }
    setItem(type,key,item){
        var mcacheClient = new MemcachePlus();
        mcacheClient.set("TP:IDX","3");
    }

    getItem(key){
        var mcacheClient = new MemcachePlus();
        mcacheClient.get("TP:IDX").on((data, status, headers, config) => {
              console.log(data.data);
              this.getVal= data;    // <-- make this value available to the class
            });
    }

    theVal(){
        return this.getVal;
    }

}
terary
  • 940
  • 13
  • 30
  • 1
    you want a blocking get in nodejs? – Jaromanda X Jan 15 '18 at 01:52
  • I have a class that calls memcache.get() -- I need to assign the value returned to a class member. – terary Jan 15 '18 at 01:56
  • without any code it's hard to imagine what you want, though, "blocking" is rarely what you want in nodejs - learn to "cope" with asynchronous code – Jaromanda X Jan 15 '18 at 01:57
  • 1
    What memcache client are you using? I cannot find `.on` in documentation [here](http://memcache-plus.com/get.html). You could look for synchronous memcache client but not sure if anyone would create such a thing. You can set `this.val` to a promise and consume it with `yourObjectInstnce.val.then(actualValue=>....)` – HMR Jan 15 '18 at 03:46
  • In the api of the class fixed? If changing the api is allowed, you can pass a callback to `getItem` function or make it return a promise. – Abhishek Nair Jan 15 '18 at 05:00

1 Answers1

1

Your question basically translate to:

What is the best way to jump off a sky scraper into a tank full of sharks with lasers on their heads.

The answer would basically be: "You don't"

Why ECMAScript uses callbacks and promises and async await syntax (is basically promises with a different syntax) is explained here with links to documents and a very good video.

Assuming you are using this memecached client you can set the value member to a promise, the code is kind of confusing since you don't set value when you call setItem. GetItem sets value but I think it's better if you name it lastRetreivedValue (and maybe add lastSetItem depending on your needs):

class TPCacheManager{
  constructor(){
      this.lastRetrievedValue = Promise.reject("No item has been set");
  }
  setItem(type,key,item){
      var mcacheClient = new MemcachePlus();
      //the caller may want to know when it's finished and if it failed
      return mcacheClient.set("TP:IDX","3");
  }

  getItem(key){
      var mcacheClient = new MemcachePlus();
      const me = this;
      //caller may want to know when it's finished and if it failed
      return mcacheClient.get("TP:IDX").
      then(value => {
        console.log(value);
        me.lastRetrievedValue= value;
        //you may want to return value here so o.getItem().then(value
        //actually resolves to something other than undefinded
      });
  }

  theVal(){
      return this.lastRetrievedValue;
  }

}

//example how to use:
const cache = new TPCacheManager();

cache.lastRetrievedValue.catch(
  err=>{
    console.log("As expected, no value has been set",err);
  }
);
cache.setItem("doesnt matter, you ignore all parameters here")
.catch(err=>console.error("Oops, something went wrong setting an item:",err));

cache.lastRetrievedValue
.catch(
  err=>
    console.log("Value still not set, setItem does not do this in your code",err)
);

cache.getItem("doesnt matter, you are ignoring parameters here")
.then(
  value=>
    console.log("Value is undefined because getItem resolves to undefined",value)
);

cache.lastRetrievedValue
.then(
  value=>
    console.log("Ok, I have value because getItem has set it:",value)
);
//you can repeat cache.lastRetrievedValueor cache.theVal() without connecting to
//  memcached because you stored the promise
HMR
  • 37,593
  • 24
  • 91
  • 160
  • Thank you for your efforts. However, 'console.log("Ok, I have value because getItem has set it:",value)' Never seems to execute.?? – terary Jan 15 '18 at 18:21
  • @terary what happens if you catch it? `cache.lastRetrievedValue.catch(err=>console.error(err))` – HMR Jan 15 '18 at 18:24