55 */
66class Mem implements MemInterface
77{
8+ public const DEFAULT_CONFIG = [
9+ 'length_limit ' => -1 ,
10+ ];
11+
812 /**
913 * Everything is stored here
10- *
11- * @var array
14+ * @var Storage[]
1215 */
1316 protected static array $ storage = [];
1417
18+ /**
19+ * @var array
20+ */
21+ protected static array $ configs = [];
22+
23+ /**
24+ * @param string $group
25+ * @param array|null|false $config
26+ * @return mixed|void|null
27+ */
28+ public static function config ($ group = 'default ' , $ config = false )
29+ {
30+ if ($ config === false ) {
31+ return isset (static ::$ configs [$ group ]) ? static ::$ configs [$ group ] : null ;
32+ }
33+
34+ if ($ config === null ) {
35+ unset(static ::$ configs [$ group ]);
36+ } else {
37+ static ::$ configs [$ group ] = $ config ;
38+ }
39+ }
40+
41+ public static function configProp ($ name , $ group = 'default ' )
42+ {
43+ return isset (static ::$ configs [$ group ][$ name ])
44+ ? static ::$ configs [$ group ][$ name ]
45+ : (isset (static ::DEFAULT_CONFIG [$ name ]) ? static ::DEFAULT_CONFIG [$ name ] : null );
46+ }
47+
48+
49+ /**
50+ * @param string $key
51+ * @param string $group
52+ * @return bool
53+ */
1554 public static function has ($ key , $ group = 'default ' )
1655 {
1756 return static ::hasGroup ($ group ) && isset (static ::$ storage [$ group ][$ key ]);
1857 }
1958
20- public static function hasGroup ($ group )
59+ /**
60+ * @param string $group
61+ * @return bool
62+ */
63+ public static function hasGroup ($ group = 'default ' )
2164 {
2265 return isset (static ::$ storage [$ group ]);
2366 }
2467
68+ /**
69+ * @param string $key
70+ * @param string $group
71+ * @param mixed $default
72+ * @return mixed|null
73+ */
2574 public static function get ($ key , $ group = 'default ' , $ default = null )
2675 {
2776 return static ::has ($ key , $ group ) ? static ::$ storage [$ group ][$ key ] : $ default ;
2877 }
2978
79+ /**
80+ * @param string $key
81+ * @param mixed $value
82+ * @param string $group
83+ * @return void
84+ */
3085 public static function set ($ key , $ value , $ group = 'default ' )
3186 {
3287 if (!static ::hasGroup ($ group )) {
33- static ::$ storage [$ group ] = [] ;
88+ static ::$ storage [$ group ] = new Storage () ;
3489 }
90+
3591 static ::$ storage [$ group ][$ key ] = $ value ;
92+
93+ $ length_limit = static ::configProp ('length_limit ' , $ group );
94+ if ($ length_limit > 0 && static ::$ storage [$ group ]->count () > $ length_limit ) {
95+ static ::$ storage [$ group ]->del (static ::$ storage [$ group ]->firstKey ());
96+ }
3697 }
3798
99+ /**
100+ * @param string $key
101+ * @param string $group
102+ * @return bool
103+ */
38104 public static function del ($ key , $ group = 'default ' )
39105 {
40106 if (static ::has ($ key , $ group )) {
@@ -45,21 +111,35 @@ public static function del($key, $group = 'default')
45111 return false ;
46112 }
47113
114+ /**
115+ * @return Storage[]
116+ */
48117 public static function all ()
49118 {
50119 return static ::$ storage ;
51120 }
52121
122+ /**
123+ * @param string $group
124+ * @return Storage|null
125+ */
53126 public static function group ($ group = 'default ' )
54127 {
55128 return static ::hasGroup ($ group ) ? static ::$ storage [$ group ] : null ;
56129 }
57130
131+ /**
132+ * @return int
133+ */
58134 public static function groupsCount ()
59135 {
60136 return count (array_keys (static ::$ storage ));
61137 }
62138
139+ /**
140+ * @param $group
141+ * @return bool
142+ */
63143 public static function drop ($ group = 'default ' )
64144 {
65145 if (static ::hasGroup ($ group )) {
@@ -70,6 +150,9 @@ public static function drop($group = 'default')
70150 return false ;
71151 }
72152
153+ /**
154+ * @return bool
155+ */
73156 public static function reset ()
74157 {
75158 if (static ::groupsCount ()) {
0 commit comments