Module: Couchbase::Operations::Touch

Defined in:
lib/couchbase/operations/touch.rb

Instance Method Summary collapse

Instance Method Details

#async_touch(key, ttl) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/couchbase/operations/touch.rb', line 108

def async_touch(key, ttl)
  if block_given?
    register_future(client.touch(key, ttl), { op: :touch }, &Proc.new)
  else
    client.touch(key, ttl)
  end
end

#touch(key, options = {}) {|ret| ... } ⇒ true, false #touch(keys) {|ret| ... } ⇒ Hash

Update the expiry time of an item

The touch method allow you to update the expiration time on a given key. This can be useful for situations where you want to prevent an item from expiring without resetting the associated value. For example, for a session database you might want to keep the session alive in the database each time the user accesses a web page without explicitly updating the session value, keeping the user’s session active and available.

Overloads:

  • #touch(key, options = {}) {|ret| ... } ⇒ true, false

    Returns true if the operation was successful and false otherwise.

    Examples:

    Touch value using default_ttl

    c.touch("foo")

    Touch value using custom TTL (10 seconds)

    c.touch("foo", :ttl => 10)

    Parameters:

    • key (String, Symbol)

      Key used to reference the value.

    • options (Hash) (defaults to: {})

      Options for operation.

    Options Hash (options):

    • :ttl (Fixnum) — default: self.default_ttl

      Expiry time for key. Values larger than 30*24*60*60 seconds (30 days) are interpreted as absolute times (from the epoch).

    • :quiet (true, false) — default: self.quiet

      If set to true, the operation won’t raise error for missing key, it will return nil.

    Yield Parameters:

    • ret (Result)

      the result of operation in asynchronous mode (valid attributes: error, operation, key).

    Returns:

    • (true, false)

      true if the operation was successful and false otherwise.

    Raises:

  • #touch(keys) {|ret| ... } ⇒ Hash

    Returns Mapping keys to result of touch operation (true if the operation was successful and false otherwise).

    Examples:

    Touch several values

    c.touch("foo" => 10, :bar => 20) #=> {"foo" => true, "bar" => true}

    Touch several values in async mode

    c.run do
      c.touch("foo" => 10, :bar => 20) do |ret|
         ret.operation   #=> :touch
         ret.success?    #=> true
         ret.key         #=> "foo" and "bar" in separate calls
      end
    end

    Touch single value

    c.touch("foo" => 10)             #=> true

    Parameters:

    • keys (Hash)

      The Hash where keys represent the keys in the database, values – the expiry times for corresponding key. See description of :ttl argument above for more information about TTL values.

    Yield Parameters:

    • ret (Result)

      the result of operation for each key in asynchronous mode (valid attributes: error, operation, key).

    Returns:

    • (Hash)

      Mapping keys to result of touch operation (true if the operation was successful and false otherwise)

Since:

  • 1.0.0



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/couchbase/operations/touch.rb', line 84

def touch(*args)
  sync_block_error if !async? && block_given?
  key, ttl, options = expand_touch_args(args)

  case key
  when String, Symbol
    if async?
      if block_given?
        async_touch(key, ttl, &Proc.new)
      else
        async_touch(key, ttl)
      end
    else
      success = client_touch(key, ttl)
      not_found_error(!success, options)
      success
    end
  when Hash
    multi_touch_hash(key, options)
  when Array
    multi_touch_array(key, options)
  end
end