# Use channel to synchronize cache

**URL:** https://discuss.tryton.org/t/use-channel-to-synchronize-cache/1062
**Category:** Feature
**Created:** [January 21, 2019, 12:31pm UTC](https://discuss.tryton.org/t/use-channel-to-synchronize-cache/1062 "2019-01-21T12:31:49Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [January 21, 2019, 12:31pm UTC](https://discuss.tryton.org/t/use-channel-to-synchronize-cache/1062/1 "2019-01-21T12:31:49Z")

</div>

## Rational

We synchronize the cache on each request (and we even tried to [reduce the number](https://bugs.tryton.org/issue7533)) but this is a little expensive knowing that such cache should be almost static. Also the commit part needs to search and update/insert row in a table and this may create conflicts.

## Proposal

Following the `Bus` design, we could have a thread daemon listening on a channel (if the database support it). The message passed to this channel is the name of the cache to clear.  
In order to prevent thread started before the clearing to fill the cache with outdated data, we need to order the transaction using a [monotonic timestamp](https://docs.python.org/3/library/time.html#time.monotonic) which is compared to the last clearing of the Cache instance.  
As this will create a database connection per process and database, this should be optional by configuration.

## Implementation

> **[Prevent older transaction to fill cache (#8019) · Issues · Tryton / Tryton ·...](https://foss.heptapod.net/tryton/tryton/-/issues/8019)**
>
> Following #7975, there are other cases where the cache could be...

> **[Use channel to synchronize Cache (#8027) · Issues · Tryton / Tryton · GitLab](https://foss.heptapod.net/tryton/tryton/-/issues/8027)**
>
> Following: https://discuss.tryton.org/t/use-channel-to-synchronize-cache/1062

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [January 22, 2019, 2:59pm UTC](https://discuss.tryton.org/t/use-channel-to-synchronize-cache/1062/2 "2019-01-22T14:59:11Z")

</div>

The patches are ready for testing. I used a clear timeout of zero to activate the channel.  
I just found during debugging that two listener thread will be launch in development mode. This is because the werkzeug simple server launch two processes when the reloader is activated. A first thread is launched when the transaction to determine the modules to load is executed and this is before the werkzeug start the new process. I do not think it is a problem.

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [March 23, 2019, 5:00pm UTC](https://discuss.tryton.org/t/use-channel-to-synchronize-cache/1062/3 "2019-03-23T17:00:09Z")

</div>

This topic was automatically closed after 14 days. New replies are no longer allowed.
