@@ -2,7 +2,7 @@ use std::{
2
2
collections:: HashMap ,
3
3
io:: { BufRead , BufReader , Write } ,
4
4
net:: TcpStream ,
5
- sync:: { Arc , Mutex } ,
5
+ sync:: { Arc , RwLock } ,
6
6
thread,
7
7
time:: Duration ,
8
8
} ;
@@ -13,7 +13,7 @@ static WRONG_TYPE_ERROR_RESPONSE: &str =
13
13
14
14
pub fn process_request (
15
15
mut stream : TcpStream ,
16
- cache : Arc < Mutex < HashMap < String , DataType > > > ,
16
+ cache : Arc < RwLock < HashMap < String , DataType > > > ,
17
17
timeout : Option < Duration > ,
18
18
) {
19
19
loop {
@@ -55,7 +55,7 @@ pub fn process_request(
55
55
}
56
56
}
57
57
58
- pub fn get_response ( cache : Arc < Mutex < HashMap < String , DataType > > > , req : & Request ) -> String {
58
+ pub fn get_response ( cache : Arc < RwLock < HashMap < String , DataType > > > , req : & Request ) -> String {
59
59
match req. command {
60
60
Command :: SET => handle_set ( req, cache) ,
61
61
Command :: GET => handle_get ( req, cache) ,
@@ -72,16 +72,16 @@ pub fn get_response(cache: Arc<Mutex<HashMap<String, DataType>>>, req: &Request)
72
72
}
73
73
}
74
74
75
- pub fn handle_set ( req : & Request , cache : Arc < Mutex < HashMap < String , DataType > > > ) -> String {
76
- let mut cache = cache. lock ( ) . unwrap ( ) ;
75
+ pub fn handle_set ( req : & Request , cache : Arc < RwLock < HashMap < String , DataType > > > ) -> String {
76
+ let mut cache = cache. write ( ) . unwrap ( ) ;
77
77
let value = req. value [ 0 ] . clone ( ) ;
78
78
cache. insert ( req. key . clone ( ) , DataType :: String ( value) ) ;
79
79
let response = "+OK\r \n " . to_string ( ) ;
80
80
response
81
81
}
82
82
83
- pub fn handle_get ( req : & Request , cache : Arc < Mutex < HashMap < String , DataType > > > ) -> String {
84
- let cache = cache. lock ( ) . unwrap ( ) ;
83
+ pub fn handle_get ( req : & Request , cache : Arc < RwLock < HashMap < String , DataType > > > ) -> String {
84
+ let cache = cache. read ( ) . unwrap ( ) ;
85
85
let request_value = cache. get ( & req. key ) ;
86
86
match request_value {
87
87
Some ( value) => match value {
@@ -102,8 +102,8 @@ pub fn handle_get(req: &Request, cache: Arc<Mutex<HashMap<String, DataType>>>) -
102
102
}
103
103
}
104
104
105
- pub fn handle_rpush ( req : & Request , cache : Arc < Mutex < HashMap < String , DataType > > > ) -> String {
106
- let mut cache = cache. lock ( ) . unwrap ( ) ;
105
+ pub fn handle_rpush ( req : & Request , cache : Arc < RwLock < HashMap < String , DataType > > > ) -> String {
106
+ let mut cache = cache. write ( ) . unwrap ( ) ;
107
107
let value = cache. get_mut ( & req. key ) ;
108
108
match value {
109
109
Some ( existing) => match existing {
@@ -128,8 +128,8 @@ pub fn handle_rpush(req: &Request, cache: Arc<Mutex<HashMap<String, DataType>>>)
128
128
}
129
129
}
130
130
131
- pub fn handle_lrange ( req : & Request , cache : Arc < Mutex < HashMap < String , DataType > > > ) -> String {
132
- let cache = cache. lock ( ) . unwrap ( ) ;
131
+ pub fn handle_lrange ( req : & Request , cache : Arc < RwLock < HashMap < String , DataType > > > ) -> String {
132
+ let cache = cache. read ( ) . unwrap ( ) ;
133
133
let value = cache. get ( & req. key ) ;
134
134
match value {
135
135
Some ( existing) => match existing {
@@ -165,8 +165,8 @@ pub fn handle_lrange(req: &Request, cache: Arc<Mutex<HashMap<String, DataType>>>
165
165
}
166
166
}
167
167
168
- pub fn handle_del ( req : & Request , cache : Arc < Mutex < HashMap < String , DataType > > > ) -> String {
169
- let mut cache = cache. lock ( ) . unwrap ( ) ;
168
+ pub fn handle_del ( req : & Request , cache : Arc < RwLock < HashMap < String , DataType > > > ) -> String {
169
+ let mut cache = cache. write ( ) . unwrap ( ) ;
170
170
let key_to_delete = cache. get ( & req. key ) ;
171
171
match key_to_delete {
172
172
Some ( _) => {
@@ -181,8 +181,8 @@ pub fn handle_del(req: &Request, cache: Arc<Mutex<HashMap<String, DataType>>>) -
181
181
}
182
182
}
183
183
184
- pub fn handle_incr ( req : & Request , cache : Arc < Mutex < HashMap < String , DataType > > > ) -> String {
185
- let mut cache = cache. lock ( ) . unwrap ( ) ;
184
+ pub fn handle_incr ( req : & Request , cache : Arc < RwLock < HashMap < String , DataType > > > ) -> String {
185
+ let mut cache = cache. write ( ) . unwrap ( ) ;
186
186
let value = cache. get ( & req. key ) ;
187
187
match value {
188
188
Some ( v) => match v {
@@ -214,8 +214,8 @@ pub fn handle_incr(req: &Request, cache: Arc<Mutex<HashMap<String, DataType>>>)
214
214
}
215
215
}
216
216
217
- pub fn handle_incrby ( req : & Request , cache : Arc < Mutex < HashMap < String , DataType > > > ) -> String {
218
- let mut cache = cache. lock ( ) . unwrap ( ) ;
217
+ pub fn handle_incrby ( req : & Request , cache : Arc < RwLock < HashMap < String , DataType > > > ) -> String {
218
+ let mut cache = cache. write ( ) . unwrap ( ) ;
219
219
let value = cache. get ( & req. key ) ;
220
220
let amount = req. value [ 0 ] . parse :: < i64 > ( ) . unwrap ( ) ;
221
221
match value {
@@ -248,8 +248,8 @@ pub fn handle_incrby(req: &Request, cache: Arc<Mutex<HashMap<String, DataType>>>
248
248
}
249
249
}
250
250
251
- pub fn handle_decr ( req : & Request , cache : Arc < Mutex < HashMap < String , DataType > > > ) -> String {
252
- let mut cache = cache. lock ( ) . unwrap ( ) ;
251
+ pub fn handle_decr ( req : & Request , cache : Arc < RwLock < HashMap < String , DataType > > > ) -> String {
252
+ let mut cache = cache. write ( ) . unwrap ( ) ;
253
253
let value = cache. get ( & req. key ) ;
254
254
match value {
255
255
Some ( v) => match v {
@@ -281,8 +281,8 @@ pub fn handle_decr(req: &Request, cache: Arc<Mutex<HashMap<String, DataType>>>)
281
281
}
282
282
}
283
283
284
- pub fn handle_decrby ( req : & Request , cache : Arc < Mutex < HashMap < String , DataType > > > ) -> String {
285
- let mut cache = cache. lock ( ) . unwrap ( ) ;
284
+ pub fn handle_decrby ( req : & Request , cache : Arc < RwLock < HashMap < String , DataType > > > ) -> String {
285
+ let mut cache = cache. write ( ) . unwrap ( ) ;
286
286
let value = cache. get ( & req. key ) ;
287
287
let amount = req. value [ 0 ] . parse :: < i64 > ( ) . unwrap ( ) ;
288
288
match value {
@@ -325,15 +325,15 @@ pub fn handle_command() -> String {
325
325
response
326
326
}
327
327
328
- pub fn handle_expire ( req : & Request , cache : Arc < Mutex < HashMap < String , DataType > > > ) -> String {
328
+ pub fn handle_expire ( req : & Request , cache : Arc < RwLock < HashMap < String , DataType > > > ) -> String {
329
329
let key = req. key . clone ( ) ;
330
330
let seconds = req. value [ 0 ] . parse :: < u64 > ( ) . unwrap ( ) ;
331
- let unlocked_cache = cache. lock ( ) . unwrap ( ) ;
331
+ let unlocked_cache = cache. read ( ) . unwrap ( ) ;
332
332
let value = unlocked_cache. get ( & req. key ) . clone ( ) ;
333
333
let cloned_cache = Arc :: clone ( & cache) ;
334
334
thread:: spawn ( move || {
335
335
thread:: sleep ( Duration :: from_secs ( seconds) ) ;
336
- let mut cache = cloned_cache. lock ( ) . unwrap ( ) ;
336
+ let mut cache = cloned_cache. write ( ) . unwrap ( ) ;
337
337
let value = cache. get ( & key) . clone ( ) ;
338
338
match value {
339
339
Some ( _) => {
0 commit comments