Memcache and Twitterstruct ID

2 views
Skip to first unread message

Cody S

unread,
Mar 9, 2010, 7:42:52 PM3/9/10
to Grackle Development
Not sure if this is a memcache topic or Grackle topic. But when
reading the results from a Grackle request from memcache the ID field
doesn't show up.

I'm guessing it has to do with the field being named id. Anyone using
grackle with memcached care to share how they store the openstruct
data returned into memcache?

Thanks!

Hayes Davis

unread,
Mar 9, 2010, 10:29:46 PM3/9/10
to grac...@googlegroups.com
Cody,

That has happened to me before but I honestly didn't look into it too closely at the time. I assumed it had to do with marshalling and methods named id. My workaround was just to set id to something else prior to inserting into the cache and then setting it back when I got it out of the cache, e.g.

obj._id = obj.id #obj is an OpenStruct so this just brings _id into existence
cache.write('key',obj)

obj = cache.read('key')
obj.id = obj._id

Obviously that's seriously inelegant so your email reminded me to look into this more closely. It turns out that it is related to the marshalling but not in the way I thought. The OpenStruct subclass, TwitterStruct, returned by Grackle methods defines an attr_accessor :id so that an attribute named id can be successfully set and retrieved on the OpenStruct. If I hadn't done that, the id on the TwitterStruct gets set but the getter always returns the object's internal id (since id is defined on all Objects). However, it introduces a problem because OpenStruct actually interjects itself into the marshaling process and the id attr_accessor means that the id attribute actually gets bypassed during dumping and loading as defined on OpenStruct.

So anyway, here's a monkey patch to fix this annoyance. I'll get this into an official release in the near future:

class TwitterStruct < OpenStruct

  def id
    _id
  end
 
  def id=(val)
    self._id = val
  end

end

It looks weird but it ensures that the _id attribute gets set on the OpenStruct so that the value for id gets marshaled and unmarshaled correctly.

Hayes

Cody S

unread,
Mar 10, 2010, 4:35:15 PM3/10/10
to Grackle Development
Thanks Hayes!

Reply all
Reply to author
Forward
0 new messages