��# S m a r t C a c h e
S m a r t C a c h e i s a s i m p l e , e f f i c i e n t c a c h i n g s y s t e m i m p l e m e n t e d i n T y p e S c r i p t .
# # F e a t u r e s
- C u s t o m i z a b l e c a c h e e x p i r y t i m e s
- O p t i o n t o k e e p e x p i r e d c a c h e d a t a
- C a l l b a c k f u n c t i o n f o r w h e n d a t a i s r e m o v e d
- R e g u l a r c a c h e i n v a l i d a t i o n
# # I n s t a l l a t i o n
U s e t h e p a c k a g e m a n a g e r [ n p m ] ( h t t p s : / / w w w . n p m j s . c o m / ) t o i n s t a l l S m a r t C a c h e .
` ` ` b a s h
n p m i n s t a l l @ x c y t h / s m a r t c a c h e
` ` `
# # U s a g e
` ` ` t y p e s c r i p t
/ *
* C o p y r i g h t ( c ) R i t a m C h o u d h u r i 2 0 2 4 .
* /
/ / I m p o r t i n g t h e C a c h e c l a s s f r o m t h e i n d e x f i l e
i m p o r t C a c h e f r o m ' . / i n d e x ' ;
/ / C r e a t i n g a n e w i n s t a n c e o f t h e C a c h e c l a s s w i t h s o m e o p t i o n s
c o n s t c a c h e = n e w C a c h e ( {
m a x E n t r i e s : 1 0 , / / M a x i m u m n u m b e r o f e n t r i e s i n t h e c a c h e
e x p i r e s I n : 1 0 0 0 0 , / / G l o b a l e x p i r y t i m e f o r a l l c a c h e e n t r i e s
o n R e m o v e : ( k e y , v a l u e ) = > { / / C a l l b a c k f u n c t i o n t o b e e x e c u t e d w h e n a n e n t r y i s r e m o v e d
c o n s o l e . l o g ( ` C a c h e v a l u e w i t h k e y : $ { k e y } r e m o v e d , v a l u e : $ { v a l u e } ` )
} ,
s a f e M o d e : t r u e , / / S a f e m o d e e n a b l e d
k e e p E x p i r e d : t r u e , / / K e e p e x p i r e d e n t r i e s i n t h e c a c h e
g l o b a l E x p i r y : 1 0 0 0 0 / / G l o b a l e x p i r y t i m e f o r a l l c a c h e e n t r i e s
} ) ;
/ / S e t t i n g a c a c h e e n t r y w i t h k e y ' k e y 1 ' a n d v a l u e ' v a l u e 1 ' w i t h a n e x p i r y t i m e o f 5 0 0 0 m s
c a c h e . s e t ( ' k e y 1 ' , ' v a l u e 1 ' , { e x p i r y : 5 0 0 0 } ) ;
t r y {
/ / A t t e m p t i n g t o s e t a c o m p l e x c a c h e e n t r y w i t h k e y ' e a '
c a c h e . s e t ( ' e a ' , {
b i n : [ 0 o 1 1 0 0 1 0 1 , 0 o 1 1 0 0 0 0 1 , 0 o 1 1 1 0 0 1 1 , 0 o 1 1 1 0 1 0 0 , 0 o 1 1 0 0 1 0 1 , 0 o 1 1 1 0 0 1 0 , 0 o 0 1 0 0 0 0 0 , 0 o 1 1 0 0 1 0 1 , 0 o 1 1 0 0 1 1 1 , 0 o 1 1 0 0 1 1 1 , 0 o 0 1 1 1 1 1 1 ] ,
d e c : [ 1 0 1 , 9 7 , 1 1 5 , 1 1 6 , 1 0 1 , 1 1 4 , 3 2 , 1 0 1 , 1 0 3 , 1 0 3 , 6 3 ] ,
h e x : [ 0 x 6 5 , 0 x 6 1 , 0 x 7 3 , 0 x 7 4 , 0 x 6 5 , 0 x 7 2 , 0 x 2 0 , 0 x 6 5 , 0 x 6 7 , 0 x 6 7 , 0 x 3 F ] ,
o c t : [ 0 o 1 4 5 , 0 o 1 4 1 , 0 o 1 6 3 , 0 o 1 6 4 , 0 o 1 4 5 , 0 o 1 6 2 , 0 o 4 0 , 0 o 1 4 5 , 0 o 1 4 7 , 0 o 1 4 7 , 0 o 7 7 ] ,
s t r : ' ? ? ? '
} ) ;
} c a t c h ( e ) {
/ / C a t c h i n g a n d l o g g i n g a n y e r r o r s t h a t o c c u r w h e n s e t t i n g t h e c a c h e e n t r y
c o n s o l e . l o g ( " M a y b e i t ' s s u p p o s e d t o t h r o w a n e r r o r , m a y b e n o t . W h o k n o w s ? " )
}
/ / S e t t i n g a c a c h e e n t r y w i t h k e y ' k e y 3 ' a n d v a l u e 8 5 5 4 1 2 6 6 5 2 w i t h a n e x p i r y t i m e o f 2 5 0 0 0 m s
c a c h e . s e t ( ' k e y 3 ' , 8 5 5 4 1 2 6 6 5 2 , { e x p i r y : 2 5 0 0 0 } ) ;
/ / L o g g i n g t h e v a l u e s o f s o m e c a c h e e n t r i e s
c o n s o l e . l o g ( c a c h e . g e t ( ' k e y 1 ' ) ) ;
c o n s o l e . l o g ( c a c h e . g e t ( ' e a ' ) ) ;
c o n s o l e . l o g ( c a c h e . g e t ( ' k e y 3 ' ) ) ;
/ / L o g g i n g t h e s i z e o f t h e c a c h e
c o n s o l e . l o g ( c a c h e . s i z e ) ;
` ` `
# # C o n t r i b u t i n g
P u l l r e q u e s t s a r e w e l c o m e . F o r m a j o r c h a n g e s , p l e a s e o p e n a n i s s u e f i r s t t o d i s c u s s w h a t y o u w o u l d l i k e t o c h a n g e .
P l e a s e m a k e s u r e t o u p d a t e t e s t s a s a p p r o p r i a t e .
# # L i c e n s e
[ M I T ] ( h t t p s : / / o p e n s o u r c e . o r g / l i c e n s e s / M I T )
@xcyth/smartcache
1.1.0 • Public • PublishedPackage Sidebar
Install
npm i @xcyth/smartcache
Repository
Weekly Downloads
6
Version
1.1.0
License
MIT
Unpacked Size
34.3 kB
Total Files
11