@@ -122,6 +122,8 @@ use serde::de::DeserializeOwned;
122122use thiserror:: Error ;
123123
124124use crate :: cache:: store:: memory:: Memory ;
125+ #[ cfg( feature = "redis" ) ]
126+ use crate :: cache:: store:: redis:: Redis ;
125127use crate :: cache:: store:: { BoxCacheStore , CacheStore } ;
126128use crate :: config:: { CacheConfig , Timeout } ;
127129use crate :: error:: error_impl:: impl_into_cot_error;
@@ -771,6 +773,11 @@ impl Cache {
771773 let mem_store = Memory :: new ( ) ;
772774 Self :: new ( mem_store, config. prefix . clone ( ) , config. timeout )
773775 }
776+ #[ cfg( feature = "redis" ) ]
777+ CacheStoreTypeConfig :: Redis { ref url, pool_size } => {
778+ let redis_store = Redis :: new ( url, pool_size) ?;
779+ Self :: new ( redis_store, config. prefix . clone ( ) , config. timeout )
780+ }
774781 _ => {
775782 unimplemented ! ( ) ;
776783 }
@@ -786,11 +793,13 @@ mod tests {
786793 use std:: fmt:: Debug ;
787794 use std:: time:: Duration ;
788795
796+ use cot:: config:: CacheUrl ;
789797 use serde:: { Deserialize , Serialize } ;
790798
791799 use super :: * ;
792800 use crate :: cache:: store:: memory:: Memory ;
793801 use crate :: config:: Timeout ;
802+ use crate :: test:: TestCache ;
794803
795804 #[ derive( Serialize , Deserialize , Debug , PartialEq ) ]
796805 struct User {
@@ -799,12 +808,14 @@ mod tests {
799808 email : String ,
800809 }
801810
802- #[ cot:: test]
803- async fn test_cache_basic_operations ( ) {
804- let store = Memory :: new ( ) ;
805- let cache = Cache :: new ( store, None , Timeout :: After ( Duration :: from_secs ( 60 ) ) ) ;
811+ #[ cot_macros:: cachetest]
812+ async fn test_cache_basic_operations ( test_cache : & mut TestCache ) {
813+ let cache = test_cache. cache ( ) ;
806814
807- cache. insert ( "user:1" , "John Doe" ) . await . unwrap ( ) ;
815+ cache
816+ . insert ( "user:1" , "John Doe" . to_string ( ) )
817+ . await
818+ . unwrap ( ) ;
808819 let user: Option < String > = cache. get ( "user:1" ) . await . unwrap ( ) ;
809820 assert_eq ! ( user, Some ( "John Doe" . to_string( ) ) ) ;
810821
@@ -827,10 +838,9 @@ mod tests {
827838 assert_eq ! ( user, Some ( "John Doe" . to_string( ) ) ) ;
828839 }
829840
830- #[ cot:: test]
831- async fn test_cache_complex_objects ( ) {
832- let store = Memory :: new ( ) ;
833- let cache = Cache :: new ( store, None , Timeout :: After ( Duration :: from_secs ( 60 ) ) ) ;
841+ #[ cot_macros:: cachetest]
842+ async fn test_cache_complex_objects ( test_cache : & mut TestCache ) {
843+ let cache = test_cache. cache ( ) ;
834844
835845 let user = User {
836846 id : 1 ,
@@ -843,10 +853,9 @@ mod tests {
843853 assert_eq ! ( cached_user, Some ( user) ) ;
844854 }
845855
846- #[ cot:: test]
847- async fn test_cache_insert_expiring ( ) {
848- let store = Memory :: new ( ) ;
849- let cache = Cache :: new ( store, None , Timeout :: After ( Duration :: from_secs ( 60 ) ) ) ;
856+ #[ cot_macros:: cachetest]
857+ async fn test_cache_insert_expiring ( test_cache : & mut TestCache ) {
858+ let cache = test_cache. cache ( ) ;
850859
851860 cache
852861 . insert_expiring (
@@ -861,13 +870,11 @@ mod tests {
861870 assert_eq ! ( value, Some ( "temporary" . to_string( ) ) ) ;
862871 }
863872
864- #[ cot:: test]
865- async fn test_cache_get_or_insert_with ( ) {
866- let store = Memory :: new ( ) ;
867- let cache = Cache :: new ( store, None , Timeout :: After ( Duration :: from_secs ( 60 ) ) ) ;
873+ #[ cot_macros:: cachetest]
874+ async fn test_cache_get_or_insert_with ( test_cache : & mut TestCache ) {
875+ let cache = test_cache. cache ( ) ;
868876
869877 let mut call_count = 0 ;
870-
871878 let value1: String = cache
872879 . get_or_insert_with ( "expensive" , || async {
873880 call_count += 1 ;
@@ -883,15 +890,13 @@ mod tests {
883890 } )
884891 . await
885892 . unwrap ( ) ;
886-
887893 assert_eq ! ( value1, value2) ;
888894 assert_eq ! ( call_count, 1 ) ;
889895 }
890896
891- #[ cot:: test]
892- async fn test_cache_get_or_insert_with_expiring ( ) {
893- let store = Memory :: new ( ) ;
894- let cache = Cache :: new ( store, None , Timeout :: After ( Duration :: from_secs ( 60 ) ) ) ;
897+ #[ cot_macros:: cachetest]
898+ async fn test_cache_get_or_insert_with_expiring ( test_cache : & mut TestCache ) {
899+ let cache = test_cache. cache ( ) ;
895900
896901 let mut call_count = 0 ;
897902
@@ -923,10 +928,9 @@ mod tests {
923928 assert_eq ! ( call_count, 1 ) ;
924929 }
925930
926- #[ cot:: test]
927- async fn test_cache_statistics ( ) {
928- let store = Memory :: new ( ) ;
929- let cache = Cache :: new ( store, None , Timeout :: After ( Duration :: from_secs ( 60 ) ) ) ;
931+ #[ cot_macros:: cachetest]
932+ async fn test_cache_statistics ( test_cache : & mut TestCache ) {
933+ let cache = test_cache. cache ( ) ;
930934
931935 assert_eq ! ( cache. approx_size( ) . await . unwrap( ) , 0 ) ;
932936
@@ -939,14 +943,34 @@ mod tests {
939943 assert_eq ! ( cache. approx_size( ) . await . unwrap( ) , 0 ) ;
940944 }
941945
942- #[ cot:: test]
943- async fn test_cache_contains_key ( ) {
944- let store = Memory :: new ( ) ;
945- let cache = Cache :: new ( store, None , Timeout :: After ( Duration :: from_secs ( 60 ) ) ) ;
946+ #[ cot_macros:: cachetest]
947+ async fn test_cache_contains_key ( test_cache : & mut TestCache ) {
948+ let cache = test_cache. cache ( ) ;
946949
947950 assert ! ( !cache. contains_key( "nonexistent" ) . await . unwrap( ) ) ;
948951
949952 cache. insert ( "existing" , "value" ) . await . unwrap ( ) ;
950953 assert ! ( cache. contains_key( "existing" ) . await . unwrap( ) ) ;
951954 }
955+
956+ #[ cfg( feature = "redis" ) ]
957+ #[ cot:: test]
958+ async fn test_cache_from_config_redis ( ) {
959+ use crate :: config:: { CacheConfig , CacheStoreConfig , CacheStoreTypeConfig } ;
960+ let url = std:: env:: var ( "REDIS_URL" ) . unwrap_or_else ( |_| "redis://localhost" . to_string ( ) ) ;
961+ let url = CacheUrl :: from ( url) ;
962+
963+ let config = CacheConfig :: builder ( )
964+ . store (
965+ CacheStoreConfig :: builder ( )
966+ . store_type ( CacheStoreTypeConfig :: Redis { url, pool_size : 5 } )
967+ . build ( ) ,
968+ )
969+ . prefix ( "test_redis" )
970+ . timeout ( Timeout :: After ( Duration :: from_secs ( 60 ) ) )
971+ . build ( ) ;
972+
973+ let result = Cache :: from_config ( & config) . await ;
974+ assert ! ( result. is_ok( ) ) ;
975+ }
952976}
0 commit comments