# Questions about Cachex

**URL:** <https://forum.elixirforum.com/t/questions-about-cachex/17531>\
**Category:** Questions\
**Tags:** cachex\
**Created:** [October 25, 2018, 7:40pm UTC](https://forum.elixirforum.com/t/questions-about-cachex/17531 "2018-10-25T19:40:17Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![apr](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/apr/32/11951_2.png) [@apr](https://forum.elixirforum.com/u/apr)\
**Post date:** [October 25, 2018, 7:40pm UTC](https://forum.elixirforum.com/t/questions-about-cachex/17531/1 "2018-10-25T19:40:17Z")

</div>

Hello!

Sorry if this is obvious, but I couldn’t find an answer in the docs.

I am using the [Cachex](https://hexdocs.pm/cachex/) library. How does one go about locking a row? Is it done via [`transaction/4`](https://hexdocs.pm/cachex/Cachex.html#transaction/4)? Is there a more lightweight version where I can just lock a row, perform some operation, and then unlock it?

Thanks!

---

<div class="post-metadata">

**Author:** ![OvermindDL1](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/overminddl1/32/2677_2.png) [@OvermindDL1](https://forum.elixirforum.com/u/OvermindDL1)\
**Post date:** [October 25, 2018, 8:51pm UTC](https://forum.elixirforum.com/t/questions-about-cachex/17531/2 "2018-10-25T20:51:12Z")

</div>

> [@apr](#):
>
> I am using the [Cachex](https://hexdocs.pm/cachex/) library. How does one go about locking a row? Is it done via [`transaction/4`](https://hexdocs.pm/cachex/Cachex.html#transaction/4)? Is there a more lightweight version where I can just lock a row, perform some operation, and then unlock it?

If by locking a row you mean locking a key/value entry? That would be an [Action Block](https://hexdocs.pm/cachex/action-blocks.html). The execution block just runs the code in the context of the cache itself, thus much faster, though not transactional across ‘everything’, the transactional is transactional on the state of the cache however. Or are you referencing something?

---

<div class="post-metadata">

**Author:** ![apr](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/apr/32/11951_2.png) [@apr](https://forum.elixirforum.com/u/apr)\
**Post date:** [October 25, 2018, 9:00pm UTC](https://forum.elixirforum.com/t/questions-about-cachex/17531/3 "2018-10-25T21:00:37Z")

</div>

Yes! Sorry for not being clear, I mean locking writes (but not reads) for a certain key.

So Action block would be the way to go? Basically I want to do something like:

```elixir
lock(:my_cache, key)
Cachex.get_and_update(:my_cache, key, fn value -> {:commit, value+1} end)
unlock(:my_cache, key)

```

Looks like Action Block suggests using [execute/3](https://hexdocs.pm/cachex/Cachex.html#execute/3). But I am not sure I see how it would prevent other processes from overwriting the key’s value in between the read and write.

---

<div class="post-metadata">

**Author:** ![OvermindDL1](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/overminddl1/32/2677_2.png) [@OvermindDL1](https://forum.elixirforum.com/u/OvermindDL1)\
**Post date:** [October 25, 2018, 10:44pm UTC](https://forum.elixirforum.com/t/questions-about-cachex/17531/4 "2018-10-25T22:44:11Z")

</div>

> [@apr](#):
>
> Looks like Action Block suggests using [execute/3](https://hexdocs.pm/cachex/Cachex.html#execute/3). But I am not sure I see how it would prevent other processes from overwriting the key’s value in between the read and write.

That’s what the Action Block `transaction` would be. `execute` just runs in the cache process for speed, but does not synchronization, transaction is what does synchronization. 🙂

---

<div class="post-metadata">

**Author:** ![apr](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/apr/32/11951_2.png) [@apr](https://forum.elixirforum.com/u/apr)\
**Post date:** [October 25, 2018, 10:55pm UTC](https://forum.elixirforum.com/t/questions-about-cachex/17531/5 "2018-10-25T22:55:27Z")

</div>

Cool! So just to confirm, that is `Cachex.transaction/4` right?

Thanks!

---

<div class="post-metadata">

**Author:** ![OvermindDL1](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/overminddl1/32/2677_2.png) [@OvermindDL1](https://forum.elixirforum.com/u/OvermindDL1)\
**Post date:** [October 25, 2018, 11:14pm UTC](https://forum.elixirforum.com/t/questions-about-cachex/17531/6 "2018-10-25T23:14:28Z")

</div>

> [@apr](#):
>
> Cool! So just to confirm, that is `Cachex.transaction/4` right?

Or `/3` if no options needed yep:  
[https://hexdocs.pm/cachex/action-blocks.html#transaction-blocks](https://hexdocs.pm/cachex/action-blocks.html#transaction-blocks)

> **[Cachex — Cachex v4.1.1](https://cachex.hexdocs.pm/Cachex.html)**

---

<div class="post-metadata">

**Author:** ![apr](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/apr/32/11951_2.png) [@apr](https://forum.elixirforum.com/u/apr)\
**Post date:** [October 25, 2018, 11:29pm UTC](https://forum.elixirforum.com/t/questions-about-cachex/17531/7 "2018-10-25T23:29:19Z")

</div>

Perfect, thanks @OvermindDL1!

---

<div class="post-metadata">

**Author:** ![apr](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/apr/32/11951_2.png) [@apr](https://forum.elixirforum.com/u/apr)\
**Post date:** [October 26, 2018, 4:52am UTC](https://forum.elixirforum.com/t/questions-about-cachex/17531/8 "2018-10-26T04:52:03Z")

</div>

Just for posterity’s sake, `Cachex.get_and_update/4` has an implicit transaction [underneath](https://github.com/whitfin/cachex/issues/190). So for this particular operation, transactions are not needed, but @OvermindDL1 's answer is the way to go for operations that do need transactions.
