apr

apr

Hello!

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

I am using the Cachex library. How does one go about locking a row? Is it done via transaction/4? Is there a more lightweight version where I can just lock a row, perform some operation, and then unlock it?

Thanks!

Showing Posts 1 to 7

OvermindDL1

OvermindDL1

If by locking a row you mean locking a key/value entry? That would be an Action Block. 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?

apr

apr OP

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:

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. 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.

OvermindDL1

OvermindDL1

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. :slight_smile:

apr

apr OP

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

Thanks!

OvermindDL1

OvermindDL1

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

apr

apr OP

Perfect, thanks @OvermindDL1!

apr

apr OP

Just for posterity’s sake, Cachex.get_and_update/4 has an implicit transaction underneath. So for this particular operation, transactions are not needed, but @OvermindDL1 's answer is the way to go for operations that do need transactions.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews