缓存泡沫对象。统一码问题
我正在使用 suds https://fedorahosted.org/suds/ 使用 SOAP 获取数据。我想缓存结果(使用 memcached),以免使我从中获取数据的服务器过载。问题出在获取缓存数据时。获取它工作正常,但随后 django 尝试解码数据(force_unicode),但失败并显示以下内容:
The string that could not be encoded/decoded was: armv�rmare t
这是我用来缓存结果的内容(其中 result = suds-object)。使用 cPickle (并尝试了 pickle 以防万一,但没有成功,这并不奇怪)。
suds 响应采用 unicode 格式。
#Cache the result
cache.set(hashstring, pickle.dumps(result), 120)
#Return the cached data
result = cache.get(hashstring)
if result:
return pickle.loads(result, encoding='utf-8')
I'm using suds https://fedorahosted.org/suds/ to fetch data using SOAP. I'd like to cache the result (using memcached) to not overload the server from where I'm fetching the data. The problem is when fetching the cached data. Fetching it works fine but then django tries to decode the data (force_unicode) and it fails with the following:
The string that could not be encoded/decoded was: armv�rmare t
This is what I use to cache the result (where result = suds-object). Using cPickle (and tried pickle just in case but without success, no surprise).
The suds response is in unicode.
#Cache the result
cache.set(hashstring, pickle.dumps(result), 120)
#Return the cached data
result = cache.get(hashstring)
if result:
return pickle.loads(result, encoding='utf-8')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Suds 响应不能轻易腌制,因为它们是动态生成的类。 Suds 使用 WSDL 为请求生成一个“架构”,通过该架构动态创建为每个方法量身定制的类。当您发出请求时,将创建此动态类的一个实例并用响应信息填充。恢复 pickle 类实例显然需要类本身,这就是 suds 的方法导致问题的地方,因为 pickle 不知道这些动态类。
一种方法是创建您自己的类来表示响应信息。您可以使用肥皂水响应填充此类的实例,并且此类将很容易腌制!
一旦信息可以pickle,就可以将其放入memcached 中。
Suds responses can't be easily pickled because they are dynamically generated classes. Suds uses the WSDL to generate a 'schema' for the request with which it dynamically creates a class tailored for each method. When you make the request, an instance of this dynamic class is created and populated with the response information. Restoring a pickled class instance obviously requires the class itself and here is where suds' approach causes problems because pickle doesn't know about these dynamic classes.
One approach for you would be to create your own classes to represent the response information. you can populate an instance of this class with the suds response and this class will be easy to pickle!
Once the information is pickle-able, it should be fine to put it into memcached.
您确信 memcached 支持复杂对象的存储吗?
另一件需要考虑的事情是 SUDS 有自己的内置缓存机制,但我相信它只是针对 WSDL/XSD 数据,这样就不必在每次启动时重新编译。然而,我并不完全乐观,它也有可能用于存储结果。
Are you positive that memcached supports storage of complex objects?
Another thing to consider is that SUDS has its own caching mechanism built-in, but I believe it is only for the WSDL/XSD data, so that it doesn't have to be recompiled every time you startup. I am not totally positive, however, and it's possible that it could also be used for storage of results.
Suds 返回一个
suds.sax.text.Text
对象,它是 Python Unicode 字符串对象的子类。我认为将该对象显式转换为 Unicode 可以解决您的问题。Suds returns a
suds.sax.text.Text
object, which is a subclass of the Python Unicode string object. I think explicitly casting that object to Unicode would solve your problem.