No cache on rust-diesel

Viewed 469

I am using MySQL as my database, and diesel to retrieve the data. The data get updated every second from multiple end-points. The problem is while using diesel, to retrieve the data, the results I get are outdated (probably due to the cache on diesel side). MySQL runs with SET GLOBAL query_cache_size = 0, so there is no active cache on the DB server-side.

Here is the part of my code to retrieve the data:

pub struct Weather {
    pub id: u32,
    pub temperature: f32,
    pub datetime: NaiveDateTime,
}

pub fn {
    let timewindow = ... // A timewindow I set
    let results = weather.filter(datetime.ge(timewindow)).load::<Weather>(&db).unwrap();
    println("{:?}", results)
}

Do you know how I can deactivate the cache on diesel?

1 Answers

The Diesel reduces boilerplate for database interactions and is the most efficient way to interact with databases in Rust because of its safe and composable abstractions over queries. Diesel offers a high level query builder and basically Diesel causes zero overhead while abstraction and tries running the query and loading the data even much faster than in C. There is no cache as such that can cause such outdated results and also it does not support SQL_NO_CACHE option related query. The outdated results seems to be related with multiple other points like query used or system setup/design as also updated in other comments.

However, if you are using prepared statements, they can improve performance while re-using the same statement that you prepared (statement will be cached for reuse by future calls to prepare_cached). In such case, you can also set the maximum number of cached prepared statements the particular connection will hold as in such case by default, a connection will hold a relatively small number of cached statements.

Related