42
38

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

memcachedコマンド操作

Last updated at Posted at 2020-08-17

##telnetで接続

# telnet localhost 11211
Trying ::1...
Connected to localhost.
Escape character is '^]'.

##データの格納

set key1 0 0 6
value1
STORED

ワンライナー

(echo "set key1 0 0 6"; sleep 0.5s; echo "value1"; sleep 0.5s;) | telnet localhost 11211

##データの取得

get key1
VALUE key1 0 6
value1
END

ワンライナー

(echo "get key1"; sleep 0.5s;) | telnet localhost 11211

##データの書換

replace key1 0 0 6
value2
STORED

##格納されているデータの後ろに追記

append key1 0 0 6
value3
STORED
get key1
VALUE key1 0 12
value2value3

##格納されているデータの前に追記

prepend key1 0 0 6
value1
STORED
get key1
VALUE key1 0 18
value1value2value3
END

##データの削除

delete key1
DELETED

##データの加算/減算

set key1 0 0 3
100
STORED
incr key1 1000
1100
decr key1 2000
0
delete key1
DELETED

##切断

quit
Connection closed by foreign host.
#

キー一覧

#!/bin/bash -eu

IFS=$'\n'

hosts=(
  localhost
)

ports=(
  11211
  11212
  11213
)

for host in ${hosts[@]}; do
  for port in ${ports[@]}; do
    keys=()   
    echo "host:${host}:${port}"
    set +e
    items=$((echo 'stats items'; sleep 0.5s;) | telnet ${host} ${port} 2>/dev/null | egrep 'items:[0-9]+:number [0-9]+')
    set -e
    for item in ${items[@]}; do
      items=$(echo $item | perl -pe 's/^STAT items:([0-9]+):.+$/$1/')
      number=$(echo $item | perl -pe 's/^.+ ([0-9]+)$/$1/')
      keys+=($((echo "stats cachedump $items $number"; sleep 0.5s;) | telnet ${host} ${port} 2>/dev/null | egrep -v 'END|^Escape.+|^Trying.+|^Connected.+'))
    done

    for key in "${keys[@]+"${keys[@]}"}"; do
      echo $key | perl -pe 's/^ITEM (.+)?/$1/'
    done
  done
done
42
38
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
42
38

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?