Using Memcache Client with Ruby

Using memcache client with ruby

Ok, for me, the problem was the security groups. You can only access Elasticache nodes from ec2 instances that have a security group that is listed in the Elasticache security group.

So for me, my ec2 instance has a security group of "web". In elasticache, I then added "web" to the "default" elasticache security group.

Further explanation here: http://docs.aws.amazon.com/AmazonElastiCache/latest/UserGuide/CacheSecurityGroup.html

Also, try using either of these two gems:

https://github.com/mperham/dalli

https://github.com/ktheory/dalli-elasticache

I'm using the latter and it works great because it uses autodiscovery of the nodes.

Memcached / Dalli couldn't fetch data from other servers

I had quick look at Daili code base. It uses consistent hashing for distributing keys to servers.

https://github.com/mperham/dalli/blob/master/lib%2Fdalli%2Fring.rb#L10

entry_count_for(server, servers.size, total_weight).times do |idx|
hash = Digest::SHA1.hexdigest("#{server.name}:#{idx}")
value = Integer("0x#{hash[0..7]}")
continuum << Dalli::Ring::Entry.new(value, server)
end

and

def entry_count_for(server, total_servers, total_weight)
((total_servers * POINTS_PER_SERVER * server.weight) / Float(total_weight)).floor
end

The keyspace of each server depends on total servers, weight and server name. So that the keyspace will be different with different total servers or server name. I think that explanation fits your problem.

Group data by Namespace in memcache

As per response received from dalli gem github tracker it is not possible to group by namespace.

Can't read key from memcached using dalli

The default Dalli timeout is 500ms. Given that memcache response times are typically single digit milliseconds, and that you put your memcache instances close to your servers this is usually ample.

On the other hand given that you are connecting via an ssh tunnel you might see much greater latencies you might want to increase the timeout:

 Dalli::Client.new("127.0.0.1:10001", socket_timeout: 2.0)

How to use Memcache and Ruby On Rails default memory store at the same time?

Rails.cache is just a cache store that is created for your convenience. There's nothing stopping you doing something like

::MEMORY_STORE = ActiveSupport::Cache::MemoryStore.new

and then when you want to use that store instead of Rails.cache you would do

MEMORY_STORE.fetch('some_key') {}

Although, as @leonardoborges commented, I'm not sure why you would want to do this



Related Topics



Leave a reply



Submit