You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/cache.md
+38Lines changed: 38 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -50,6 +50,44 @@ To create a cache with a virtually unlimited size, you can specify a `max_size`
50
50
const auto cached_query = sqlgen::cache<0>(query);
51
51
```
52
52
53
+
### Cache Invalidation
54
+
55
+
The cache has no mechanism for determining if a cached result is still valid/up-to-date. Because of this, the cache should be explicitly `clear`ed before use any time another query is made that invalidates the cache.
56
+
57
+
```cpp
58
+
#include<sqlgen.hpp>
59
+
60
+
structUser {
61
+
std::string name;
62
+
int age;
63
+
};
64
+
65
+
const auto conn = sqlgen::sqlite::connect();
66
+
67
+
const auto user = User{.name = "John", .age = 30};
68
+
sqlgen::write(conn, user);
69
+
70
+
const auto user_b = User{.name = "Mary", .age = 25};
71
+
sqlgen::write(conn, user_b);
72
+
73
+
const auto query = sqlgen::read<std::vector<User>>;
74
+
const auto cached_query = sqlgen::cache<100>(query);
75
+
76
+
const auto users1 = cached_query(conn).value();
77
+
// The cache size will now contain a result consisting of John & Mary
78
+
79
+
const auto user_c = User{.name = "Bill", .age = 50};
80
+
sqlgen::write(conn, user_c);
81
+
82
+
// Because the query was previously cached, user2 will still only contain John & Mary while Bill will be absent.
83
+
const auto users2 = cached_query(conn).value();
84
+
85
+
cached_query.clear(conn);
86
+
87
+
// Now, the query will be executed again since it's no longer cached. Afterwards, the cache will again store an up-to-date result and users3 will contain John, Mary, & Bill.
88
+
const auto users3 = cached_query(conn).value();
89
+
```
90
+
53
91
### Thread Safety and Concurrency
54
92
55
93
The cache is thread-safe and can be accessed from multiple threads concurrently. A `std::shared_mutex` is used to protect the cache from data races.
0 commit comments