How to set memcached CACHESIZE via chef based on node’s memory

Let’s say you wish to set the memcached CACHESIZE variable based on your system’s(node) memory.
By default CACHESIZE is defined as 64MB. You get the nodes total memory using the node[‘memory’][‘total’] which will return the memory in KB for example 1019492kB In this case you need to divide it with 1024 to get the size in MB. If you want to assign 10% of your total memory to memcached CACHESIZE then you ll have 1019492kB * 0.1 / 1024 = 99.559MB. You can remove the .559 using ruby’s to_i method.

So you can put the following in your cookbook to set your memcached cachesize. The only dependency is opscode memcached cookbook

node.set['memcached']['memory'] = (node['memory']['total'].to_i * 0.1 / 1024).to_i
include_recipe 'memcached'