当前位置: 首页 > OpenSource > 正文

Get memcached all items and deleted them if needed.

    I see there are so many api to operate memcached,such as get and set,but no list all items,so i write a shell just list all the items in memcached and we can delete the items when we don’t know the exactly items key.

#!bin/bash
# get_items_from_memcached.sh
# Usge: sh get_items_from_memcached.sh localhost port
# Exp:  sh get_items_from_memcached.sh 2hei.net 11211
# By: @2hei.net

items=`echo “stats items” | nc $1 $2|grep number|awk -F: ‘{print $2}’|awk ‘{printf(“%s “,$1) }’`
for i in ${items}
  do
    #get delete_items_list
    echo “stats cachedump $i 0” | nc $1 $2|awk -v HOST=$1 -v PORT=$2 ‘{if(length($2)>0) print “echo delete”,$2,” | nc”,HOST,PORT}’ >> $1_$2.txt
    #print all items
    echo “stats cachedump $i 0” | nc $1 $2|awk ‘{if($2) print $2}’
  done

##delete all items by item_list if needed
#/bin/sh $1_$2.txt

##END##

tips:
1. you just can use “flush_all” cmd
   echo “flush_all” | nc $1 $2
   
   “flush_all” is a command with an optional numeric argument. It always
succeeds, and the server sends “OK\r\n” in response (unless “noreply”
is given as the last parameter). Its effect is to invalidate all
existing items immediately (by default) or after the expiration specified.
   flush_all doesn’t actually free all the memory taken up by existing items; that
will happen gradually as new items are stored. The most precise
definition of what flush_all does is the following: it causes all
items whose update time is earlier than the time at which flush_all
was set to be executed to be ignored for retrieval purposes.

2. if your memcached has to many items, this shell will waste a long time, for it will establish a new connection when delete each time.
we can use other tools write by socket and do this in only one connection.

本文固定链接: https://www.2hei.net/2010/07/14/get_memcached_all_items_and_delete_them/ | 2hei.net

该日志由 u2 于2010年07月14日发表在 OpenSource 分类下,
原创文章转载请注明: Get memcached all items and deleted them if needed. | 2hei.net
关键字:

Get memcached all items and deleted them if needed.:目前有2 条留言