neon-mmd / websurfx

:rocket: An open source alternative to searx which provides a modern-looking :sparkles:, lightning-fast :zap:, privacy respecting :disguised_face:, secure :lock: meta search engine
https://github.com/neon-mmd/websurfx/tree/rolling/docs
GNU Affero General Public License v3.0
763 stars 97 forks source link

♻️ Redesign the `cache` internal api to a more composable design structure using enums #623

Open neon-mmd opened 1 month ago

neon-mmd commented 1 month ago

What would you like to share?

Work Expected From The Issue

Redesign the cache internal api to move it away from using an OOP based design to a more composable enum based.

The issue expects the following files to be changed/modified:

[!Note] All the files that are expected to be changed are located under the codebase (websurfx directory).

Reason Behind These Changes

The reason behind having these changes is to make the internal API design more flexible and adaptable so that future changes like providing a new caching server as a feature can introduced easily. Additionally, it adopts the rusty-style of code which encourages composability over traditional OOP design.

Sample Design API

The sample API design has been provided below to give a better idea of the work to be done:

///  --- reusing the already existing version ---
#[async_trait]
pub trait CacheTypes: Send + Sync {
    /// ----
}

///  --- reusing the already existing version ---
#[cfg(feature = "memory-cache")]
pub struct InMemoryCache {
    /// -----
}

///  --- reusing the already existing version ---
#[cfg(feature = "memory-cache")]
impl Clone for InMemoryCache {
   /// -----
}

#[cfg(feature = "memory-cache")]
impl InMemoryCache {
    async fn new(config: &Config) -> Self {
       /// ----- 
    }
}

#[cfg(feature = "memory-cache")]
#[async_trait]
impl CacheTypes for InMemoryCache {

    ///  --- reusing the already existing version ---
    async fn cached_results(&mut self, url: &str) -> Result<SearchResults, Report<CacheError>> {
       /// ----- 
    }

    ///  --- reusing the already existing version ---
    async fn cache_results(
        &mut self,
        search_results: &[SearchResults],
        urls: &[String],
    ) -> Result<(), Report<CacheError>> {
       /// ----- 
    }
}

/// TODO: New implementation
pub enum SwitchCache {
    #[cfg(feature = "redis-cache")]
    RedisCache(RedisCache),
    #[cfg(feature = "memory-cache")]
    InMemoryCache(InMemoryCache),
}

impl SwitchCache {
    /// TODO: New implementation
    async fn new(config: &Config) -> Result<Self, Box<dyn std::error::Error>> {
        todo();        
    }
}

#[async_trait]
impl CacheTypes for SwitchCache {
    /// TODO: New implementation
    async fn cached_results(&mut self, url: &str) -> Result<SearchResults, Report<CacheError>> {
        todo();
    }

    /// TODO: New implementation
    async fn cache_results(
        &mut self,
        search_results: &[SearchResults],
        urls: &[String],
    ) -> Result<(), Report<CacheError>> {
        todo();
    }
}

///  --- reusing the already existing version ---
pub struct SharedCache(Mutex<SwitchCache>);

///  --- reusing the already existing version ---
impl SharedCache {
    /// ----
}

///  --- reusing the already existing version ---
#[cfg(any(feature = "compress-cache-results", feature = "cec-cache-results"))]
async fn decompress_util(input: &[u8]) -> Result<Vec<u8>, Report<CacheError>> {
    /// ----
}

///  --- reusing the already existing version ---
#[cfg(feature = "redis-cache")]
#[async_trait::async_trait]
impl CacheTypes for RedisCache {
    /// ----
}

///  --- reusing the already existing version ---
impl TryInto<SearchResults> for Vec<u8> {
    /// ----

}

///  --- reusing the already existing version ---
impl TryInto<Vec<u8>> for &SearchResults {
    /// ----
}

Do you want to work on this issue?

None

Additional information

No response

github-actions[bot] commented 1 month ago

The issue has been unlocked and is now ready for dev. If you would like to work on this issue, you can comment to have it assigned to you. You can learn more in our contributing guide https://github.com/neon-mmd/websurfx/blob/rolling/CONTRIBUTING.md