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 | |
| 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')
| -rw-r--r-- | src/model/channel.rs | 10 | ||||
| -rw-r--r-- | src/model/guild.rs | 12 | ||||
| -rw-r--r-- | src/model/id.rs | 8 | ||||
| -rw-r--r-- | src/model/invite.rs | 4 | ||||
| -rw-r--r-- | src/model/user.rs | 4 | ||||
| -rw-r--r-- | src/model/utils.rs | 2 |
6 files changed, 20 insertions, 20 deletions
diff --git a/src/model/channel.rs b/src/model/channel.rs index 0513966..d00bec5 100644 --- a/src/model/channel.rs +++ b/src/model/channel.rs @@ -436,7 +436,7 @@ impl Message { #[cfg(feature = "methods")] pub fn delete(&self) -> Result<()> { let req = permissions::MANAGE_MESSAGES; - let is_author = self.author.id != CACHE.lock().unwrap().user.id; + let is_author = self.author.id != CACHE.read().unwrap().user.id; if is_author { return Err(Error::Client(ClientError::InvalidUser)); @@ -503,7 +503,7 @@ impl Message { return Err(Error::Client(ClientError::MessageTooLong(length_over))); } - if self.author.id != CACHE.lock().unwrap().user.id { + if self.author.id != CACHE.read().unwrap().user.id { return Err(Error::Client(ClientError::InvalidUser)); } @@ -720,7 +720,7 @@ impl PrivateChannel { /// [`ClientError::InvalidUser`]: ../client/enum.ClientError.html#variant.InvalidOperationAsUser #[cfg(feature = "methods")] pub fn delete_messages(&self, message_ids: &[MessageId]) -> Result<()> { - if !CACHE.lock().unwrap().user.bot { + if !CACHE.read().unwrap().user.bot { return Err(Error::Client(ClientError::InvalidOperationAsUser)); } @@ -889,7 +889,7 @@ impl PublicChannel { /// optimized in the future. #[cfg(all(feature = "cache", feature = "methods"))] pub fn guild(&self) -> Option<LiveGuild> { - CACHE.lock().unwrap().get_guild(self.guild_id).cloned() + CACHE.read().unwrap().get_guild(self.guild_id).cloned() } /// Return a [`Mention`] which will link to this channel. @@ -978,7 +978,7 @@ impl Reaction { /// [permissions]: permissions #[cfg(feature = "methods")] pub fn delete(&self) -> Result<()> { - let user = if self.user_id == CACHE.lock().unwrap().user.id { + let user = if self.user_id == CACHE.read().unwrap().user.id { None } else { Some(self.user_id.0) diff --git a/src/model/guild.rs b/src/model/guild.rs index 704a93b..bfe29d2 100644 --- a/src/model/guild.rs +++ b/src/model/guild.rs @@ -52,7 +52,7 @@ impl Emoji { /// [`Guild`]: struct.Guild.html #[cfg(feature = "methods")] pub fn find_guild_id(&self) -> Option<GuildId> { - CACHE.lock() + CACHE.read() .unwrap() .guilds .values() @@ -167,7 +167,7 @@ impl Guild { impl LiveGuild { #[cfg(feature = "cache")] fn has_perms(&self, mut permissions: Permissions) -> Result<bool> { - let member = match self.get_member(CACHE.lock().unwrap().user.id) { + let member = match self.get_member(CACHE.read().unwrap().user.id) { Some(member) => member, None => return Err(Error::Client(ClientError::ItemMissing)), }; @@ -379,7 +379,7 @@ impl LiveGuild { /// [`ClientError::InvalidUser`]: ../client/enum.ClientError.html#variant.InvalidUser #[cfg(feature = "methods")] pub fn delete(&self) -> Result<Guild> { - if self.owner_id != CACHE.lock().unwrap().user.id { + if self.owner_id != CACHE.read().unwrap().user.id { let req = permissions::MANAGE_GUILD; return Err(Error::Client(ClientError::InvalidPermissions(req))); @@ -856,7 +856,7 @@ impl Member { /// [`Guild`]: struct.Guild.html #[cfg(feature = "methods")] pub fn find_guild(&self) -> Result<GuildId> { - CACHE.lock() + CACHE.read() .unwrap() .guilds .values() @@ -931,7 +931,7 @@ impl Member { /// If role data can not be found for the member, then `None` is returned. #[cfg(all(feature = "cache", feature = "methods"))] pub fn roles(&self) -> Option<Vec<Role>> { - CACHE.lock().unwrap() + CACHE.read().unwrap() .guilds .values() .find(|g| g.members @@ -1028,7 +1028,7 @@ impl Role { /// [`ClientError::GuildNotFound`]: ../client/enum.ClientError.html#variant.GuildNotFound #[cfg(feature = "methods")] pub fn find_guild(&self) -> Result<GuildId> { - CACHE.lock() + CACHE.read() .unwrap() .guilds .values() 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() diff --git a/src/model/invite.rs b/src/model/invite.rs index 6552783..5216509 100644 --- a/src/model/invite.rs +++ b/src/model/invite.rs @@ -30,7 +30,7 @@ impl Invite { #[cfg(feature="methods")] pub fn accept(&self) -> Result<Invite> { feature_cache_enabled! {{ - if CACHE.lock().unwrap().user.bot { + if CACHE.read().unwrap().user.bot { return Err(Error::Client(ClientError::InvalidOperationAsBot)); } }} @@ -84,7 +84,7 @@ impl RichInvite { #[cfg(feature="methods")] pub fn accept(&self) -> Result<Invite> { feature_cache_enabled! {{ - if CACHE.lock().unwrap().user.bot { + if CACHE.read().unwrap().user.bot { return Err(Error::Client(ClientError::InvalidOperationAsBot)); } }} diff --git a/src/model/user.rs b/src/model/user.rs index 9e4c457..4030644 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -62,7 +62,7 @@ impl User { pub fn direct_message(&self, content: &str) -> Result<Message> { let private_channel_id = { - let finding = CACHE.lock() + let finding = CACHE.read() .unwrap() .private_channels .values() @@ -125,7 +125,7 @@ impl User { }, GuildContainer::Id(guild_id) => { feature_cache! {{ - let cache = CACHE.lock().unwrap(); + let cache = CACHE.read().unwrap(); cache.get_role(guild_id, role_id).is_some() } else { diff --git a/src/model/utils.rs b/src/model/utils.rs index 60fa892..d6a4cc8 100644 --- a/src/model/utils.rs +++ b/src/model/utils.rs @@ -275,7 +275,7 @@ pub fn remove(map: &mut BTreeMap<String, Value>, key: &str) -> Result<Value> { pub fn user_has_perms(channel_id: ChannelId, mut permissions: Permissions) -> Result<bool> { - let cache = CACHE.lock().unwrap(); + let cache = CACHE.read().unwrap(); let current_user = &cache.user; let channel = match cache.get_channel(channel_id) { |