Doug Ramirez
Doug Ramirez

Reputation: 41

Python memcache flush_all behavior

I'm wondering if anyone can help shed some light on why calling flush_all() doesn't seem to update stats. If I create a client, add 1 key, get it, and then flush_all() I'd expect a subsequent get_stats() to return 0 for curr_items, but it doesn't. Not until a get() of the key is performed after the flush_all() will curr_items be set back to 0.

Here's an example of what I'm witnessing:

import memcache

# Create a client
mc = memcache.Client(['127.0.0.1:11211'], debug=0)

# Add a key
mc.add('foo', 'bar')
print mc.get('foo')

# Get stats
stats = mc.get_stats()

# There will be 1 current item
print "Initial get_stat(): {}".format(stats[0][1]['curr_items'])

# Flush all
mc.flush_all()

# Get stats again
stats2 = mc.get_stats()

# There shouldn't be any items, but there is 1
print "Second get_stat(): {}".format(stats2[0][1]['curr_items'])

# Get the one key we added before
mc.get('foo')

# Get stats a third time
stats3 = mc.get_stats()

# There shouldn't be any items and now there aren't
print "Third get_stat(): {}".format(stats3[0][1]['curr_items'])

Execution results:

bar
Initial get_stat(): 1
Second get_stat(): 1
Third get_stat(): 0

Upvotes: 1

Views: 2430

Answers (1)

Barmar
Barmar

Reputation: 781058

The flush operation doesn't actually remove anything from memory, it just marks everything as expired. Expired items aren't actually removed until you try to access them.

Upvotes: 1

Related Questions