# File lib/storage/cache.rb, line 203
  def put(obj)
    return nil if obj.nil?
    @st.inc(:writes)
    hv = hash(obj.id)

    # search the cache

    @cache[hv].each do |ce|
      next if ce.oid != obj.id
      @st.inc(:cache_write_hits) if ce.dirty?
      if obj.object_id != ce.obj.object_id  # be safe

        # Be extra careful here.  It's possible we could have two objects

        # with the same object id but with different Ruby object_ids.

        # Most likely this is a bug, but we should handle it here..

        ce.obj = obj
        log.warn "Duplicate object id's in cache!"
        log.warn "insert - #{obj.inspect}"
        log.warn "cache - #{obj.inspect}"
      end
      # need to try putting hot hit to the head of the list?

      ce.dirty!
      return
    end

    # get and remove the last entry off this list

    ch = @cache[hv].pop
    # if its dirty we write it to the database

    if ch.dirty? && !ch.dead?
      # errors possible - check in store module

      @db[ch.oid.to_s] = Utility.encode(ch.obj)
      @st.inc(:database_writes)
      if ch.noswap?  # here we have a problem we can't use this

        # first push it back onto the list

        @cache[hv].unshift ch
        # get ourselves a brand new

        ch = CacheEntry.new
        # problem solved

        # the depth of any list chains will be cache_depth + # noswap entries

      end
    end

    # assign our new object to the cache entry

    ch.obj = obj
    ch.oid = obj.id
    ch.dirty!
    # push it to the head of the list

    @cache[hv].unshift ch

    return
  end