Skip to content

Updated memcache error checking to handle large object exception#748

Merged
theacodes merged 3 commits intomasterfrom
memcache-error-checking
Jan 6, 2017
Merged

Updated memcache error checking to handle large object exception#748
theacodes merged 3 commits intomasterfrom
memcache-error-checking

Conversation

@ryanmats
Copy link
Copy Markdown
Contributor

@ryanmats ryanmats commented Jan 6, 2017

See bug #33780149 on buganizer.

For some reason memcache.add throws an exception for objects larger than 1 MB rather than simpling returning False. Thus, I changed the sample code to use try/except around memcache.add, while preserving the check to see if memcache.add returned True or False. The boolean returned does seem to be necessary since it is False when memcache already has an entry for a given key.

Also got rid of 'version: 1' from the app.yaml so that 'gcloud app deploy' works.

@ryanmats ryanmats requested a review from theacodes January 6, 2017 02:52
@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Jan 6, 2017
if not memcache.add('{}:greetings'.format(guestbook_name),
greetings, 10):
logging.error('Memcache set failed.')
except:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Catch a more specific exception

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

try:
if not memcache.add('{}:greetings'.format(guestbook_name),
greetings, 10):
logging.error('Memcache set failed.')
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since these are two separate cases, you want two separate error messages. Should be, Memcache add failed, key already exists and Memcache add failed, data larger than 1MB

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a more specific message for data larger than 1MB.

For the memcache.add False result, it might not necessarily be a key already exists error, so I'm leaving it as 'Memcache set failed.'

if not memcache.add('{}:greetings'.format(guestbook_name),
greetings, 10):
try:
if not memcache.add('{}:greetings'.format(guestbook_name),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe break up this cramped up line with added = memcache.add() then if not added

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

added = memcache.add('{}:greetings'.format(guestbook_name),
greetings, 10)
if not added:
logging.error('Memcache set failed.')
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just need -value already exists here right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we know for sure if memcache.add returning False is necessarily a 'key already exists' type of error? The docs say memcache.add returns 'False on error'. (https://cloud.google.com/appengine/docs/python/refdocs/modules/google/appengine/api/memcache#Client.add). Wondering if the False could mean something else in certain circumstances.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I don't know, I guess we can leave it as is.

greetings, 10):
logging.error('Memcache set failed.')
try:
added = memcache.add('{}:greetings'.format(guestbook_name),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please avoid hanging indents, newline after add(.

logging.error('Memcache set failed.')
except ValueError:
logging.error('Memcache set failed - data larger than 1MB')
except Exception as e:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for this broad exception.

@theacodes theacodes merged commit 538078d into master Jan 6, 2017
@theacodes theacodes deleted the memcache-error-checking branch January 6, 2017 20:46
chalmerlowe pushed a commit that referenced this pull request Apr 7, 2026
* feat: add dual region bucket support and tests

* add dual region bucket sample

* fix lint

* update docstrings and doc ref links

Co-authored-by: Daniel Bankhead <dan@danielbankhead.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla: yes This human has signed the Contributor License Agreement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants