1- use crate :: ClArgs ;
2- use crate :: { errors:: ApiError , extractors:: Authentication , ApiState } ;
3- use axum:: response:: IntoResponse ;
4- use axum:: { body:: Body , extract:: State , response:: Response , Json } ;
1+ use crate :: { errors:: ApiError , extractors:: Authentication , ApiState , ClArgs } ;
2+ use axum:: { body:: Body , extract:: State , response:: IntoResponse , response:: Response , Json } ;
53use chrono:: Utc ;
64use log:: warn;
75use mini_moka:: sync:: { Cache , CacheBuilder } ;
86use reqwest:: { Client , StatusCode } ;
97use serde:: Deserialize ;
108use serde_json:: { json, Value } ;
11- use std:: fs:: read_to_string;
12- use std:: sync:: Arc ;
13- use std:: time:: Duration ;
9+ use std:: { collections:: VecDeque , fs:: read_to_string, sync:: Arc } ;
1410use tokio:: sync:: RwLock ;
1511use uuid:: Uuid ;
1612
@@ -27,11 +23,32 @@ struct Ratelimits {
2723 reset : u64 ,
2824}
2925
30- impl Default for HypixelApiProxyState {
31- fn default ( ) -> Self {
26+ impl HypixelApiProxyState {
27+ pub fn new ( cache_limit_bytes : u64 ) -> Self {
3228 Self {
33- cache : CacheBuilder :: new ( 10_000 )
34- . time_to_live ( Duration :: from_secs ( 2 * 24 * 60 * 60 ) )
29+ cache : CacheBuilder :: new ( cache_limit_bytes)
30+ . weigher ( |_, value| {
31+ let mut size = size_of :: < Uuid > ( ) as u32 + size_of :: < Value > ( ) as u32 ;
32+ let mut recursive_search = VecDeque :: from ( [ value] ) ;
33+ while let Some ( value) = recursive_search. pop_front ( ) {
34+ match value {
35+ Value :: String ( string) => size += string. capacity ( ) as u32 ,
36+ Value :: Array ( vec) => {
37+ size += ( vec. capacity ( ) * size_of :: < Value > ( ) ) as u32 ;
38+ recursive_search. extend ( vec) ;
39+ }
40+ Value :: Object ( map) => {
41+ size += map. keys ( ) . map ( String :: len) . sum :: < usize > ( ) as u32 ;
42+ size += ( ( size_of :: < String > ( ) + size_of :: < Value > ( ) ) * map. len ( ) ) as u32 ; // Capacity isn't available?
43+ recursive_search. extend ( map. values ( ) ) ;
44+ }
45+ _ => { }
46+ } ;
47+ }
48+ size
49+ } )
50+ // Disable Time to Live for now
51+ // .time_to_live(Duration::from_secs(2 * 24 * 60 * 60))
3552 . build ( ) ,
3653 ratelimits : RwLock :: new ( Ratelimits {
3754 limit : 10 ,
0 commit comments