PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, November 13, 2022

[FIXED] how to get a string from memcach on appengine - java

 November 13, 2022     google-app-engine, java, memcached     No comments   

Issue

I'm using memcache on appengine. I inserted a json string:

private static CacheFactory cacheFactory = CacheManager.getInstance().getCacheFactory();;
private static Cache cache = cacheFactory.createCache(Collections.emptyMap());;
String data = "{\name\": \"jim\"}";
cache.put("1234", data);

now I'm trying to get the string from memcache and create a json object:

JSONObject dataObj =   new JSONObject(cache.get("1234")); // log  => {name: "john"}

this works for me well, and I get the expected JSON -> {name: "john"}.

Now I am getting the data before I create the json;

Object myData = cache.get("1234");
JSONObject dataObj =   new JSONObject(myData); // log => {"bytes":[123,34,110,97,109,101,34,58,34,106,111,104,110,34,125],"empty":false}

and now I am receiving the data in an byte Object -> {bytes: [12,..],empty: false}

What's going on? Isn't there an expected behavior?

EDIT I will note that when on the memcach-console the value is saved ok -> {name: "john"}


Solution

Instead of

Object myData = cache.get("1234");

try

String myData = (String) cache.get("1234");

By the way, on App Engine I am using the following code:

MemcacheService memcache = MemcacheServiceFactory.getMemcacheService();
memcache.put("1234", object);
String myData = (String) memcache.get("1234");


Answered By - Andrei Volgin
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing