cloudant / python-cloudant

A Python library for Cloudant and CouchDB
Apache License 2.0
163 stars 55 forks source link

Provide a way to disable cache #411

Closed AyWa closed 6 years ago

AyWa commented 6 years ago

Bug Description

Sorry if I miss understand anything. But from what I see, myDb[id] will cache in the dict the result of the query. However I do not want to cache anything

1. Steps to reproduce and the simplest code sample possible to demonstrate the issue

couchClient= CouchDB(url=XXX)
couchClient.connect()
myDb = couchClient["DB_NAME"]
myDb[id]

2. What you expected to happen

I expect to have a way to disable caching (some option on get ? or on the db ?)

Environment details

If there is a need of a pr, I might take a look, lets just agree on the api ;)

alfinkel commented 6 years ago

If you're interested in a fresh Document every time then you can use the Document.fetch method.

AyWa commented 6 years ago

@alfinkel thank you for your answer

however fetch is also adding the new document to cache

populates the locally cached Document object with that content

However my goal is to not have cache at all to not have memory "leak" (I only get document once during my python script)

alfinkel commented 6 years ago

Based on that, it would seem as though you're probably more interested in having a Result object rather than a Document returned. Using a Result would circumvent caching in the database dict.

FWIW, an undocumented feature of a database object is that it has a result attribute pointing to the primary index of the database. You can use this result to retrieve your desired document content based on the document id.

ricellis commented 6 years ago

This is effectively a duplicate of https://github.com/cloudant/python-cloudant/issues/67 (see also discussion on https://github.com/cloudant/python-cloudant/pull/277).

The dicts are a fundamental part of the legacy design of this library and we won't be changing that in the foreseeable future. We will almost certainly be dropping the dict cache behaviour entirely in a future release.

Anyway, for now, in addition to Al's suggestion the user is entirely able to control the dict and can easily call del db['docid'] to remove a doc from the dict or del client['db'] to remove an entire database. You could issue those calls immediately following your fetch or destroy the local dict of the db after a few operations, whatever suits your application needs.

AyWa commented 6 years ago

Thank you for the answer :) For now I was calling db.clear()