11import socket
22import json
3+ import zlib
34from struct import unpack , pack
45
56
@@ -28,6 +29,10 @@ def cache_add(element_id, data, data_type, timeout=None, compress=None, user_id=
2829 try :
2930 cp = SocketHandler ()
3031 cp .connect (socket_file , server , port )
32+ except Exception as ex :
33+ return Response ({"success" : False , "message" : "Unable to connect to cache server. Error message: " + str (ex )})
34+
35+ try :
3136 # Prepare package
3237 data = {
3338 'target' : "add" ,
@@ -42,14 +47,18 @@ def cache_add(element_id, data, data_type, timeout=None, compress=None, user_id=
4247 response = cp .send_data (data , buffer_size = buffer_size )
4348 cp .close ()
4449 return Response (json .loads (response ))
45- except :
46- return Response ({"success" : False , "message" : "Unable to connect to cache server." })
50+ except Exception as ex :
51+ return Response ({"success" : False , "message" : "Unable to add element to cache. Error message: " + str ( ex ) })
4752
4853
4954def cache_get (element_id = None , data_type = None , user_id = None , reset_timeout = False , timeout = None , socket_file = None , server = "localhost" , port = 4444 , buffer_size = 4096 ):
5055 try :
5156 cp = SocketHandler ()
5257 cp .connect (socket_file , server , port )
58+ except Exception as ex :
59+ return Response ({"success" : False , "message" : "Unable to connect to cache server. Error message: " + str (ex )})
60+
61+ try :
5362 # Prepare package
5463 data = {
5564 'target' : "get" ,
@@ -63,19 +72,22 @@ def cache_get(element_id=None, data_type=None, user_id=None, reset_timeout=False
6372 response = cp .get_data (data , buffer_size = buffer_size )
6473 cp .close ()
6574 return Response (json .loads (response ))
66- except :
67- return Response ({"success" : False , "message" : "Unable to connect to cache server." })
75+ except Exception as ex :
76+ return Response ({"success" : False , "message" : "Unable to get element from cache. Error message: " + str ( ex ) })
6877
6978
7079def cache_remove (element_id , user_id = None , return_elem = False , socket_file = None , server = "localhost" , port = 4444 , buffer_size = 4096 ):
80+ # Get data
81+ if return_elem :
82+ element = cache_get (element_id = element_id , user_id = user_id , socket_file = socket_file , server = server , port = port , buffer_size = buffer_size )
83+
7184 try :
72- # Get data
73- if return_elem :
74- element = cache_get (element_id = element_id , user_id = user_id , socket_file = socket_file , server = server , port = port , buffer_size = buffer_size )
75- # Remove element
7685 cp = SocketHandler ()
7786 cp .connect (socket_file , server , port )
78- print ("sadasds" )
87+ except Exception as ex :
88+ return Response ({"success" : False , "message" : "Unable to connect to cache server. Error message: " + str (ex )})
89+
90+ try :
7991 # Prepare package
8092 data = {
8193 'target' : "remove" ,
@@ -90,14 +102,18 @@ def cache_remove(element_id, user_id=None, return_elem=False, socket_file=None,
90102
91103 cp .close ()
92104 return Response (response )
93- except :
94- return Response ({"success" : False , "message" : "Unable to connect to cache server." })
105+ except Exception as ex :
106+ return Response ({"success" : False , "message" : "Unable to remove element from cache. Error message: " + str ( ex ) })
95107
96108
97109def cache_reset (element_id , timeout = None , user_id = None , socket_file = None , server = "localhost" , port = 4444 , buffer_size = 4096 ):
98110 try :
99111 cp = SocketHandler ()
100112 cp .connect (socket_file , server , port )
113+ except Exception as ex :
114+ return Response ({"success" : False , "message" : "Unable to connect to cache server. Error message: " + str (ex )})
115+
116+ try :
101117 # Prepare package
102118 data = {
103119 'target' : "reset" ,
@@ -109,16 +125,16 @@ def cache_reset(element_id, timeout=None, user_id=None, socket_file=None, server
109125 response = cp .send_data (data , buffer_size = buffer_size )
110126 cp .close ()
111127 return Response (json .loads (response ))
112- except :
113- return Response ({"success" : False , "message" : "Unable to connect to cache server." })
128+ except Exception as ex :
129+ return Response ({"success" : False , "message" : "Unable to reset element at cache. Error message: " + str ( ex ) })
114130
115131
116132class SocketHandler :
117133 def __init__ (self ):
118134 self .socket = None
119135
120136 def connect (self , socket_file = None , server_ip = None , server_port = None ):
121- if socket_file is not None :
137+ if socket_file is not None and socket_file != "" :
122138 self .socket = socket .socket (socket .AF_UNIX , socket .SOCK_STREAM )
123139 self .socket .connect (socket_file )
124140 else :
@@ -131,7 +147,7 @@ def close(self):
131147 self .socket = None
132148
133149 def send_data (self , data , buffer_size = 4096 ):
134- data = json .dumps (data )
150+ data = zlib . compress ( json .dumps (data ) )
135151 # use struct to make sure we have a consistent endianness on the length
136152 length = pack ('>Q' , len (data ))
137153 # sendall to make sure it blocks if there's back-pressure on the socket
@@ -146,10 +162,10 @@ def send_data(self, data, buffer_size=4096):
146162 # to do it all in one go, so I believe.
147163 to_read = data_length - len (data )
148164 data += self .socket .recv (buffer_size if to_read > buffer_size else to_read )
149- return data
165+ return zlib . decompress ( data )
150166
151167 def get_data (self , data , buffer_size = 4096 ):
152- data = json .dumps (data )
168+ data = zlib . compress ( json .dumps (data ) )
153169 # use struct to make sure we have a consistent endianness on the length
154170 length = pack ('>Q' , len (data ))
155171 # sendall to make sure it blocks if there's back-pressure on the socket
@@ -164,7 +180,7 @@ def get_data(self, data, buffer_size=4096):
164180 # to do it all in one go, so I believe.
165181 to_read = data_length - len (data )
166182 data += self .socket .recv (buffer_size if to_read > buffer_size else to_read )
167- return data
183+ return zlib . decompress ( data )
168184
169185
170186class Response (object ):
@@ -200,5 +216,3 @@ def __str__(self):
200216
201217 def __repr__ (self ):
202218 return self .__str__ ()
203-
204-
0 commit comments