Index

Ways to Write Data

set() vs. ensure() vs. fill()

To understand these methods, lets' use a metaphore. Imagine a table with nothing on it. On the floor is a bucket, inside of which is a banana. Next to the bucket is an apple.

Using set() is akin to placing the bucket on the table, removing the banana and putting the apple inside the bucket.

ensure()

Start with an empty table, an empty bucket on the floor and the apple. After calling ensure, we have a bucket with the apple in it on the table.

Now consider the same scenario but the bucket has the banana in it to start with, and the bucket is on the floor. After calling ensure, we have a bucket still with a banana in it, but the bucket is on the table.

Lastly, think of the table with a bucket on it containing the banana. After calling ensure you would see no change.

When you call ensure(), you ensure there is a bucket on the table, if not you put one there. Then we look inside the bucket on the table to see if it's empty, if so we place the apple inside of it.

fill()

If you understand ensure() then you should be able to get fill() rather quickly.

Calling fill will not place the bucket on the table, only fill it if it's already on the table AND it's empty. How a bucket is considered empty is configurable, so refer to the docblocks for that.

set()

So with those out of the way, set() is a piece of cake.

Set doesn't care if there is a bucket on the table or not. Set will walk up to the table grab the bucket, dump it out, set it on the table, and put the apple in it. No consideration of anything.

In Summary

set ensure fill
guarantees key/property Y Y -
overwrites current value Y - only if empty