← 返回 reddit 的题目列表Implement a Simplified Memcached Protocol
类型:online_judge
Problem: Implement a Simplified Memcached Protocol
Memcached is a networked in-memory key-value store. Implement the core logic of a simplified Memcached-like protocol.
For this problem, to make testing easy, read one client session from stdin and write the server responses to stdout. You may assume all input is valid.
Protocol Rules
Commands are ASCII encoded.
Commands are line-based. Each command line ends with \n.
Commands are case-sensitive.
A key is a non-empty string containing only A-Z, a-z, 0-9, _, and -.
A value can be arbitrary bytes, including newline characters.
Supported Commands
1. get key_name
Look up the value for the given key.
If the key exists, return:
VALUE key_name byte_count
raw_data
END
Where:
byte_count is the byte length of the value.
raw_data is exactly byte_count bytes.
After the raw value data, output one newline, then END\n.
If the key does not exist, return only:
END
2. set key_name byte_count
Store a value for the given key.
After this command line, the client sends:
byte_count bytes of raw data
\n
The server should read exactly byte_count bytes as the value and consume the following newline.
After storing the value, output:
STORED
3. Bonus: get key_name [key_name ...]
get may accept multiple keys.
Return all existing keys in the requested order, each as a VALUE block, and then output one final END\n.
Missing keys are skipped.
Constraints
Number of commands: 1 <= n <= 10^5
Key length: 1 <= len(key) <= 250
Single value length: 0 <= byte_count <= 10^6
Total input size fits in memory.
Example
Input:
set foo 5
hello
get foo
get missing
Output:
STORED
VALUE foo 5
hello
END
END
Example
Input
get does_not_exist
Output
END