Hi, Rodrigo,

FWIW, I sympathize with your symbols-vs-strings as keys frustration, but =
I think it's not so trivial to have the best of both worlds (flexibility =
and performance).  Here is a simplification of your example:

require 'redis'
cache =3D Redis.new
value =3D :value
key =3D 'key'

v1 =3D cache[key] || (cache[key] =3D value)
v2 =3D cache[key] || (cache[key] =3D value)

p v1                   # :value or "value"
p v2                   # always "value"
p cache[key] =3D=3D value  # always false

IMHO, the crux of the problem here is that Redis converts all Symbols to =
Strings, but assigning "v1 =3D cache[key] =3D value" does not store a =
String in v1 if value is a Symbol.  This could be addressed by doing:

v1 =3D cache[key] || (cache[key] =3D value; cache[key])

which will store a String in v1 if value is a Symbol even if key does =
not yet exist in cache.  Yes, it is an extra cache fetch.

The same kind of thing happens if value is a Fixnum.  Surely you =
wouldn't want Fixnums and Strings to be the same thing! :-)

Thanks for starting this fascinating thread,
Dave

On Feb 6, 2013, at 3:45 PM, Rodrigo Rosenfeld Rosas wrote:

> Ok, you missed the point. Let me show you a complete example as I =
think it will help you understanding my concerns (I thought I made =
myself clear in the ticket description, but I hope this code example =
will help you understand what I meant):
>=20
> require 'redis'
> require 'json'
> require 'sequel'
>=20
> cache =3D Redis.new
> users =3D cache['users'] || begin
>  db =3D Sequel.connect('postgres://user:password@localhost/mydb')
>  cache['users'] =3D db[:users].select(:id, :name).map{|r| id: r[:id], =
name: r[:name]} # or just select(:id, :name).all
> end
>=20
> p users.first[:id]
>=20
>=20
> What will be the output of the code above?
>=20
> Exactly! It depends! If the results were cached it will print "nil", =
otherwise it will print "1" (or whatever the first id is).
>=20
> This is the problem that symbols cause because they don't behave like =
strings.
>=20
> Best,
> Rodrigo.