# ETS queue - how to create a cache for ui?

**URL:** <https://erlangforums.com/t/ets-queue-how-to-create-a-cache-for-ui/1838>\
**Category:** Questions / Help\
**Tags:** ets\
**Created:** [September 13, 2022, 11:29pm UTC](https://erlangforums.com/t/ets-queue-how-to-create-a-cache-for-ui/1838 "2022-09-13T23:29:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![apoorv-2204](https://erlangforums.com/user_avatar/erlangforums.com/apoorv-2204/32/1629_2.png) [@apoorv-2204](https://erlangforums.com/u/apoorv-2204)\
**Post date:** [September 13, 2022, 11:29pm UTC](https://erlangforums.com/t/ets-queue-how-to-create-a-cache-for-ui/1838/1 "2022-09-13T23:29:55Z")

</div>

Hello I want to create a cache for ui.  
key: index and value approx\_size : 1 mb.

So I want to have 100 index and once filled , like a queue we eject the txns from bottom.  
How to convert ets into queue.

Thank you

---

<div class="post-metadata">

**Author:** ![mmin](https://erlangforums.com/user_avatar/erlangforums.com/mmin/32/2193_2.png) [@mmin](https://erlangforums.com/u/mmin)\
**Post date:** [September 14, 2022, 8:28am UTC](https://erlangforums.com/t/ets-queue-how-to-create-a-cache-for-ui/1838/2 "2022-09-14T08:28:28Z")

</div>

Disclaimer: Never done something like this and I’m not sure if I got this right.

One could model queue as gen\_statem or gen\_server, with its state maintaining start and end indices. So you could call something like: my\_queue:insert(Elem), which would call gen\_server:send to some fixed name like e.g. ‘$ui\_queue’ which would store the element in ets with key e.g. {$ui\_queue, index}

Just my 2 cents 😃

---

<div class="post-metadata">

**Author:** ![Maria-12648430](https://erlangforums.com/user_avatar/erlangforums.com/maria-12648430/32/604_2.png) [@Maria-12648430](https://erlangforums.com/u/Maria-12648430)\
**Post date:** [September 14, 2022, 8:43am UTC](https://erlangforums.com/t/ets-queue-how-to-create-a-cache-for-ui/1838/3 "2022-09-14T08:43:02Z")

</div>

You can’t “convert” ETS to a queue 😉 You will need a managing process to do what (I think) you want to do. A bonus to that is that you can more or less hide away ETS that way, and even slap on a nice API.

(I’m not sure if tossing around 1MB-sized terms would be a problem performance- and otherwise, but that is another matter 😅)

* * *

The way I would do it is roughly like this:

Have a managing process, `gen_server` or `gen_statem` (pick one as you see fit) that owns an ETS table.

The managing process should keep two “pointers” in its state, integers denoting the front and rear indexes of the elements it manages; start with both set to `0`.

When you push an item in, store it in the ETS table using the rear pointers’ value as its index (key) and increase the rear pointer by 1.

When you pull an item out (by which you also delete it), get and delete (maybe use `ets:take/2`, which does both in one operation) the item with the index of the front pointers’ value, and increase the front pointer by 1.

If both front and rear pointers have the same value, the queue is empty btw, nothing to retrieve then, you should take that into account ☝.

The size of the queue can be calculated simply by subtracting the two pointers.

If, on an insert operation, you find that you have hit the maximum number of elements you want to keep, just drop the element at the front from the table (and increase the front pointer) before doing the actual insert as described above.

* * *

@juhlig and I did something similar with [GitHub - hnc-agency/shq: Shared inter-process queues](https://github.com/hnc-agency/shq), which is a queue much like what you ask for, but instead of dropping elements it blocks/rejects insert operations until there is enough room. It is a bit complicated and the internal workings are hidden in a lot of managing code (for blocks/rejects, waiting, etc), that is why I explained it again here.

---

<div class="post-metadata">

**Author:** ![Maria-12648430](https://erlangforums.com/user_avatar/erlangforums.com/maria-12648430/32/604_2.png) [@Maria-12648430](https://erlangforums.com/u/Maria-12648430)\
**Post date:** [September 14, 2022, 8:46am UTC](https://erlangforums.com/t/ets-queue-how-to-create-a-cache-for-ui/1838/4 "2022-09-14T08:46:41Z")

</div>

> [@mmin](#):
>
> queue:insert(Elem)

That would clash with the `queue` module in the stdlib 😉

---

<div class="post-metadata">

**Author:** ![mmin](https://erlangforums.com/user_avatar/erlangforums.com/mmin/32/2193_2.png) [@mmin](https://erlangforums.com/u/mmin)\
**Post date:** [September 14, 2022, 8:51am UTC](https://erlangforums.com/t/ets-queue-how-to-create-a-cache-for-ui/1838/5 "2022-09-14T08:51:05Z")

</div>

ofc, let’s call it “my\_queue” for example 😃

---

<div class="post-metadata">

**Author:** ![mworrell](https://erlangforums.com/user_avatar/erlangforums.com/mworrell/32/267_2.png) [@mworrell](https://erlangforums.com/u/mworrell)\
**Post date:** [September 14, 2022, 8:56am UTC](https://erlangforums.com/t/ets-queue-how-to-create-a-cache-for-ui/1838/6 "2022-09-14T08:56:09Z")

</div>

I am not sure, but the OP seems to ask for a caching system with automatic cleanup once a certain size is reached.

You could use depcache for that, it starts ejecting after a certain size (in MB) threshold has been reached.

> **[depcache](https://hex.pm/packages/depcache)**
>
> In-memory cache with cache key dependencies

---

<div class="post-metadata">

**Author:** ![Maria-12648430](https://erlangforums.com/user_avatar/erlangforums.com/maria-12648430/32/604_2.png) [@Maria-12648430](https://erlangforums.com/u/Maria-12648430)\
**Post date:** [September 14, 2022, 9:06am UTC](https://erlangforums.com/t/ets-queue-how-to-create-a-cache-for-ui/1838/7 "2022-09-14T09:06:53Z")

</div>

Yeah, it is not entirely clear what is wanted. But now there are answers for both possible interpretations 😉

---

<div class="post-metadata">

**Author:** ![juhlig](https://erlangforums.com/user_avatar/erlangforums.com/juhlig/32/837_2.png) [@juhlig](https://erlangforums.com/u/juhlig)\
**Post date:** [September 14, 2022, 9:13am UTC](https://erlangforums.com/t/ets-queue-how-to-create-a-cache-for-ui/1838/8 "2022-09-14T09:13:25Z")

</div>

Interestingly enough, it wouldn’t _clash_ 🤣

You _can_ have a module like `queue` in your project. Whichever happens to get loaded first is what is used, across the runtime system, whenever something calls `queue:...`

Don’t ask me why I know this, I don’t remember 😅

---

<div class="post-metadata">

**Author:** ![Maria-12648430](https://erlangforums.com/user_avatar/erlangforums.com/maria-12648430/32/604_2.png) [@Maria-12648430](https://erlangforums.com/u/Maria-12648430)\
**Post date:** [September 14, 2022, 9:17am UTC](https://erlangforums.com/t/ets-queue-how-to-create-a-cache-for-ui/1838/9 "2022-09-14T09:17:11Z")

</div>

@juhlig, don’t put crazy ideas in peoples’ heads 😳

> [@juhlig](#):
>
> You _can_ have a module like `queue` in your project. Whichever happens to get loaded first is what is used, across the runtime system, whenever something calls `queue:...`

Kids, don’t do that at home. Seriously, I mean it.

> [@juhlig](#):
>
> Don’t ask me why I know this, I don’t remember 😅

I think I have a pretty good idea… 🫣
