diff options
| author | Austin Hellyer <[email protected]> | 2016-11-22 21:20:46 -0800 |
|---|---|---|
| committer | Austin Hellyer <[email protected]> | 2016-11-22 21:20:46 -0800 |
| commit | 41820989b48fe803867e19cadde557ea090f99bd (patch) | |
| tree | 601480f55c0358ae0963257307cbd2019423dec0 /src/model/id.rs | |
| parent | Rename the State to Cache (diff) | |
| download | serenity-41820989b48fe803867e19cadde557ea090f99bd.tar.xz serenity-41820989b48fe803867e19cadde557ea090f99bd.zip | |
Change the CACHE to be an RwLock
The global Cache used to be an Arc<Mutex>, however the issue is that it
could only be opened for reading or writing once at a time.
With an RwLock, multiple readers can access the Cache at once, while
only one Writer may at once. This will allow users to be able to have
multiple Readers open at once, which should ease some of the pains with
working with the Cache.
Upgrade path:
Modify all uses of the CACHE from:
`CACHE.lock().unwrap()`
to
`CACHE.read().unwrap()` if reading from the Cache (most use cases), or
`CACHE.write().unwrap()` to write to it.
Diffstat (limited to 'src/model/id.rs')
| -rw-r--r-- | src/model/id.rs | 8 |
1 files changed, 4 insertions, 4 deletions
diff --git a/src/model/id.rs b/src/model/id.rs index f055784..d9c131e 100644 --- a/src/model/id.rs +++ b/src/model/id.rs @@ -9,14 +9,14 @@ impl ChannelId { /// Search the cache for the channel with the Id. #[cfg(feature="methods")] pub fn find(&self) -> Option<Channel> { - CACHE.lock().unwrap().get_channel(*self) + CACHE.read().unwrap().get_channel(*self) } /// Search the cache for the channel. If it can't be found, the channel is /// requested over REST. #[cfg(feature="methods")] pub fn get(&self) -> Result<Channel> { - if let Some(channel) = CACHE.lock().unwrap().get_channel(*self) { + if let Some(channel) = CACHE.read().unwrap().get_channel(*self) { return Ok(channel.clone()); } @@ -77,7 +77,7 @@ impl GuildId { /// Search the cache for the guild. #[cfg(feature="methods")] pub fn find(&self) -> Option<LiveGuild> { - CACHE.lock().unwrap().get_guild(*self).cloned() + CACHE.read().unwrap().get_guild(*self).cloned() } /// Requests the guild over REST. @@ -163,7 +163,7 @@ impl RoleId { /// Search the cache for the role. #[cfg(feature="methods")] pub fn find(&self) -> Option<Role> { - CACHE.lock() + CACHE.read() .unwrap() .guilds .values() |