Skip to content

Commit 8bd878b

Browse files
lift cache key
1 parent 4ffabc2 commit 8bd878b

File tree

2 files changed

+41
-39
lines changed

2 files changed

+41
-39
lines changed

crates/pg_schema_cache/src/schema_cache.rs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ use crate::versions::Version;
99

1010
#[derive(Debug, Clone, Default)]
1111
pub struct SchemaCache {
12-
cached_conn_str: String,
13-
1412
pub schemas: Vec<Schema>,
1513
pub tables: Vec<Table>,
1614
pub functions: Vec<Function>,
@@ -31,7 +29,6 @@ impl SchemaCache {
3129
)?;
3230

3331
Ok(SchemaCache {
34-
cached_conn_str: SchemaCache::pool_to_conn_str(pool),
3532
schemas,
3633
tables,
3734
functions,
@@ -41,30 +38,6 @@ impl SchemaCache {
4138
})
4239
}
4340

44-
fn pool_to_conn_str(pool: &PgPool) -> String {
45-
let conn = pool.connect_options();
46-
47-
match conn.get_database() {
48-
None => format!(
49-
"postgres://{}:<redacted_pw>@{}:{}",
50-
conn.get_username(),
51-
conn.get_host(),
52-
conn.get_port()
53-
),
54-
Some(db) => format!(
55-
"postgres://{}:<redacted_pw>@{}:{}/{}",
56-
conn.get_username(),
57-
conn.get_host(),
58-
conn.get_port(),
59-
db
60-
),
61-
}
62-
}
63-
64-
pub fn has_already_cached_connection(&self, pool: &PgPool) -> bool {
65-
self.cached_conn_str == SchemaCache::pool_to_conn_str(pool)
66-
}
67-
6841
/// Applies an AST node to the repository
6942
///
7043
/// For example, alter table add column will add the column to the table if it does not exist

crates/pg_workspace/src/workspace/server/schema_cache_manager.rs

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,48 +7,77 @@ use crate::WorkspaceError;
77

88
use super::async_helper::run_async;
99

10-
#[derive(Debug)]
1110
pub(crate) struct SchemaCacheHandle<'a> {
12-
inner: RwLockReadGuard<'a, SchemaCache>,
11+
inner: RwLockReadGuard<'a, SchemaCacheManagerInner>,
1312
}
1413

1514
impl<'a> SchemaCacheHandle<'a> {
16-
pub(crate) fn new(cache: &'a RwLock<SchemaCache>) -> Self {
15+
pub(crate) fn new(cache: &'a RwLock<SchemaCacheManagerInner>) -> Self {
1716
Self {
1817
inner: cache.read().unwrap(),
1918
}
2019
}
2120

22-
pub(crate) fn wrap(inner: RwLockReadGuard<'a, SchemaCache>) -> Self {
21+
pub(crate) fn wrap(inner: RwLockReadGuard<'a, SchemaCacheManagerInner>) -> Self {
2322
Self { inner }
2423
}
2524
}
2625

2726
impl AsRef<SchemaCache> for SchemaCacheHandle<'_> {
2827
fn as_ref(&self) -> &SchemaCache {
29-
&self.inner
28+
&self.inner.cache
3029
}
3130
}
3231

32+
#[derive(Default)]
33+
pub(crate) struct SchemaCacheManagerInner {
34+
cache: SchemaCache,
35+
conn_str: String,
36+
}
37+
3338
#[derive(Default)]
3439
pub struct SchemaCacheManager {
35-
cache: RwLock<SchemaCache>,
40+
inner: RwLock<SchemaCacheManagerInner>,
3641
}
3742

3843
impl SchemaCacheManager {
3944
pub fn load(&self, pool: PgPool) -> Result<SchemaCacheHandle, WorkspaceError> {
40-
let cache_lock = self.cache.read().unwrap();
45+
let inner = self.inner.read().unwrap();
4146

42-
if cache_lock.has_already_cached_connection(&pool) {
43-
Ok(SchemaCacheHandle::wrap(cache_lock))
47+
if pool_to_conn_str(&pool) == inner.conn_str {
48+
Ok(SchemaCacheHandle::wrap(inner))
4449
} else {
50+
let new_conn_str = pool_to_conn_str(&pool);
51+
4552
let maybe_refreshed = run_async(async move { SchemaCache::load(&pool).await })?;
4653
let refreshed = maybe_refreshed?;
4754

48-
let mut cache = self.cache.write().unwrap();
49-
*cache = refreshed;
55+
let mut inner = self.inner.write().unwrap();
56+
57+
inner.cache = refreshed;
58+
inner.conn_str = new_conn_str;
5059

51-
Ok(SchemaCacheHandle::new(&self.cache))
60+
Ok(SchemaCacheHandle::new(&self.inner))
5261
}
5362
}
5463
}
64+
65+
fn pool_to_conn_str(pool: &PgPool) -> String {
66+
let conn = pool.connect_options();
67+
68+
match conn.get_database() {
69+
None => format!(
70+
"postgres://{}:<redacted_pw>@{}:{}",
71+
conn.get_username(),
72+
conn.get_host(),
73+
conn.get_port()
74+
),
75+
Some(db) => format!(
76+
"postgres://{}:<redacted_pw>@{}:{}/{}",
77+
conn.get_username(),
78+
conn.get_host(),
79+
conn.get_port(),
80+
db
81+
),
82+
}
83+
}

0 commit comments

Comments
 (0)