@@ -111,6 +111,41 @@ pub unsafe fn set_cache_object_limit(kind: ObjectType, size: libc::size_t) -> Re
111111 Ok ( ( ) )
112112}
113113
114+ /// Set the maximum total data size that will be cached in memory across all
115+ /// repositories before libgit2 starts evicting objects from the cache. This
116+ /// is a soft limit, in that the library might briefly exceed it, but will start
117+ /// aggressively evicting objects from cache when that happens. The default
118+ /// cache size is 256MB.
119+ ///
120+ /// # Safety
121+ /// This function is modifying a C global without synchronization, so it is not
122+ /// thread safe, and should only be called before any thread is spawned.
123+ pub unsafe fn set_cache_max_size ( size : libc:: ssize_t ) -> Result < ( ) , Error > {
124+ crate :: init ( ) ;
125+ try_call ! ( raw:: git_libgit2_opts(
126+ raw:: GIT_OPT_SET_CACHE_MAX_SIZE as libc:: c_int,
127+ size
128+ ) ) ;
129+ Ok ( ( ) )
130+ }
131+
132+ /// Get the current bytes in cache and the maximum that would be allowed in the cache.
133+ ///
134+ /// # Safety
135+ /// This function is reading a C global without synchronization, so it is not
136+ /// thread safe, and should only be called before any thread is spawned.
137+ pub unsafe fn get_cached_memory ( ) -> Result < ( libc:: ssize_t , libc:: ssize_t ) , Error > {
138+ crate :: init ( ) ;
139+ let mut current = 0 ;
140+ let mut allowed = 0 ;
141+ try_call ! ( raw:: git_libgit2_opts(
142+ raw:: GIT_OPT_GET_CACHED_MEMORY as libc:: c_int,
143+ & mut current,
144+ & mut allowed
145+ ) ) ;
146+ Ok ( ( current, allowed) )
147+ }
148+
114149/// Controls whether or not libgit2 will verify when writing an object that all
115150/// objects it references are valid. Enabled by default, but disabling this can
116151/// significantly improve performance, at the cost of potentially allowing the
@@ -484,4 +519,12 @@ mod test {
484519 assert ! ( get_server_timeout_in_milliseconds( ) . unwrap( ) == 10_000 ) ;
485520 }
486521 }
522+
523+ #[ test]
524+ fn cache_size ( ) {
525+ unsafe {
526+ assert ! ( set_cache_max_size( 20 * 1024 * 1024 ) . is_ok( ) ) ;
527+ assert ! ( get_cached_memory( ) . is_ok_and( |m| m. 1 == 20 * 1024 * 1024 ) ) ;
528+ }
529+ }
487530}
0 commit comments