Sites

Stupid coding mistakes

Made a really annoying mistake today while writing some code to delete data out of Redis.

Ruby

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

defdelete_keys

cursor="0"

loopdo

cursor,keys=redis.scan(cursor,count:SCAN_COUNT)

keys.reject!{|key|keep_key?(key)}

redis.del(keys)unlesskeys.empty?

breakifcursor=="0"

end

end

defkeep_key?(key)

keep_keys.any?{|keep_key|key.include?(keep_key)}

end

defkeep_keys

flattened_keys<<transaction_id

end

The mistake was in the final method keep_keys. Every check to see if a key should be rejected I was adding an element to the flattened_keys array over and over again, causing my deletion to slow down over time. A simple change to memoize the keep keys made the process go from never finishing to, completing in a few seconds.