jeremyevans/sequel — pull request #639 by rintaun (2013-04-06 03:14:40+00:00)
Title: Rescue potential caching exception on delete

Currently, if a row which has not been cached is updated, Memcached will raise
a Memcached::NotFound exception, as the key does not exist in the cache.

A demonstration of this bug is below.

``` ruby
require 'pg'
require 'sequel'
require 'memcached'
require 'logger'

CACHE = Memcached.new(ENV['MEMCACHED_HOST'])
LOG = Logger.new $stdout

DB = Sequel.connect(ENV['DATABASE_URL'])
DB.loggers << LOG
DB.create_table :tests do
primary_key :id
String :name
end

class Test < Sequel::Model
Sequel::Model.plugin :caching, CACHE, :ignore_exceptions => true
end

test = nil
begin
DB.transaction do
test = Test.new do |t|
t.name = 'foo'
t.save
end
test.update :name => 'bar'
end
rescue StandardError => e
LOG.error e.message
end

DB.drop_table :tests
```

With caching enabled the log looks something like this:

```
I, [2013-04-05T22:11:09.412921 #5161] INFO -- : (0.000240s) BEGIN
I, [2013-04-05T22:11:09.414387 #5161] INFO -- : (0.000718s) INSERT INTO "tests" ("name") VALUES ('foo') RETURNING *
I, [2013-04-05T22:11:09.416146 #5161] INFO -- : (0.000267s) ROLLBACK
E, [2013-04-05T22:11:09.416434 #5161] ERROR -- : Memcached::NotFound
I, [2013-04-05T22:11:09.432665 #5161] INFO -- : (0.016016s) DROP TABLE "tests"
```

Without caching, it works as expected:

```
I, [2013-04-05T22:12:05.797347 #11195] INFO -- : (0.000397s) BEGIN
I, [2013-04-05T22:12:05.799824 #11195] INFO -- : (0.001353s) INSERT INTO "tests" ("name") VALUES ('foo') RETURNING *
I, [2013-04-05T22:12:05.802866 #11195] INFO -- : (0.001030s) UPDATE "tests" SET "name" = 'bar' WHERE ("id" = 1)
I, [2013-04-05T22:12:05.813578 #11195] INFO -- : (0.010343s) COMMIT
I, [2013-04-05T22:12:05.831456 #11195] INFO -- : (0.017399s) DROP TABLE "tests"
```