Skip to content

Commit

Permalink
Merge pull request #1286 from HTGAzureX1212/nightly
Browse files Browse the repository at this point in the history
Cache: Fix Cache Updater for Guild Create
  • Loading branch information
HTGAzureX1212 authored Aug 8, 2023
2 parents 295d260 + 91c4637 commit ac4dc69
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl CacheUpdater for GuildCreate {
CachedGuildRepository.upsert(entity).await?;

for role in &self.0.roles {
CachedRoleRepository.upsert(RoleEntity::from(role.clone())).await?;
CachedRoleRepository.upsert(RoleEntity::from((self.0.id, role.clone()))).await?;
}

Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,24 @@

use hartex_discord_core::discord::model::guild::Role;
use hartex_discord_core::discord::model::id::Id;
use hartex_discord_core::discord::model::id::marker::GuildMarker;
use hartex_discord_core::discord::model::id::marker::RoleMarker;
use hartex_discord_entitycache_core::Entity;

#[allow(clippy::module_name_repetitions)]
#[derive(Entity)]
pub struct RoleEntity {
#[entity(id)]
pub guild_id: Id<GuildMarker>,
#[entity(id)]
pub id: Id<RoleMarker>,
pub name: String,
}

impl From<Role> for RoleEntity {
fn from(role: Role) -> Self {
impl From<(Id<GuildMarker>, Role)> for RoleEntity {
fn from((guild_id, role): (Id<GuildMarker>, Role)) -> Self {
Self {
guild_id,
id: role.id,
name: role.name
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@
* with HarTex. If not, see <https://www.gnu.org/licenses/>.
*/

use std::env;

use hartex_discord_entitycache_core::error::CacheResult;
use hartex_discord_entitycache_core::traits::Entity;
use hartex_discord_entitycache_core::traits::Repository;
use hartex_discord_entitycache_entities::role::RoleEntity;
use redis::AsyncCommands;
use redis::Client;

pub struct CachedRoleRepository;

Expand All @@ -32,7 +36,20 @@ impl Repository<RoleEntity> for CachedRoleRepository {
todo!()
}

async fn upsert(&self, _: RoleEntity) -> CacheResult<()> {
todo!()
async fn upsert(&self, entity: RoleEntity) -> CacheResult<()> {
let pass = env::var("DOCKER_REDIS_REQUIREPASS")?;
let client = Client::open(format!("redis://:{pass}@127.0.0.1/"))?;
let mut connection = client.get_tokio_connection().await?;
connection
.set(
format!(
"guild:{}:role:{}:name",
entity.guild_id, entity.id
),
entity.name,
)
.await?;

Ok(())
}
}

0 comments on commit ac4dc69

Please sign in to comment.