Show
Ignore:
Timestamp:
04/01/08 15:16:49 (8 months ago)
Author:
ryan@…
Branch:
default
Message:

Added: cas/gets, and noreply option on sets

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • jmemcached-core/src/main/java/com/thimbleware/jmemcached/Cache.java

    r10 r14  
    3232    public int getMisses; 
    3333 
     34    public long casCounter; 
     35 
    3436    protected CacheStorage cacheStorage; 
     37 
     38 
     39    public enum StoreResponse { 
     40        STORED, NOT_STORED, EXISTS, NOT_FOUND 
     41    } 
    3542 
    3643 
     
    4047     */ 
    4148    private final ReadWriteLock cacheReadWriteLock; 
    42  
    4349 
    4450    /** 
     
    9096     * @return the message response string 
    9197     */ 
    92     protected boolean add(MCElement e) { 
    93         try { 
    94             startCacheRead(); 
    95             return !is_there(e.keystring) && set(e); 
     98    protected StoreResponse add(MCElement e) { 
     99        try { 
     100            startCacheRead(); 
     101            if (is_there(e.keystring)) return set(e); 
     102            else return StoreResponse.NOT_STORED; 
    96103        } finally { 
    97104            finishCacheRead(); 
     
    105112     * @return the message response string 
    106113     */ 
    107     public boolean replace(MCElement e) { 
    108         try { 
    109             startCacheRead(); 
    110             return is_there(e.keystring) && set(e); 
    111         } finally { 
    112             finishCacheRead(); 
    113         } 
    114     } 
     114    public StoreResponse replace(MCElement e) { 
     115        try { 
     116            startCacheRead(); 
     117            if (is_there(e.keystring)) return set(e); 
     118            else return StoreResponse.NOT_STORED; 
     119        } finally { 
     120            finishCacheRead(); 
     121        } 
     122    } 
     123 
    115124 
    116125    /** 
     
    120129     * @return the message response string 
    121130     */ 
    122     protected boolean set(MCElement e) { 
     131    protected StoreResponse set(MCElement e) { 
    123132        try { 
    124133            startCacheWrite(); 
    125134            setCmds += 1;//update stats 
     135 
     136            // increment the CAS counter; put in the new CAS 
     137            e.cas_unique = casCounter++; 
     138 
    126139            this.cacheStorage.put(e.keystring, e); 
    127             return true; 
     140            return StoreResponse.STORED; 
     141        } finally { 
     142            finishCacheWrite(); 
     143        } 
     144    } 
     145 
     146    public StoreResponse cas(Long cas_key, MCElement e) { 
     147        try { 
     148            startCacheWrite(); 
     149            // have to get the element 
     150            MCElement element = get(e.keystring); 
     151            if (element == null) 
     152                return StoreResponse.NOT_FOUND; 
     153 
     154            if (element.cas_unique == cas_key) { 
     155                // cas_unique matches, now set the element 
     156                return set(e); 
     157            } else { 
     158                // cas didn't match; someone else beat us to it 
     159                return StoreResponse.EXISTS; 
     160            } 
     161 
    128162        } finally { 
    129163            finishCacheWrite(); 
     
    157191            e.data = valueOf(old_val).getBytes(); // toString 
    158192            e.data_length = e.data.length; 
     193 
     194            // assign new cas id 
     195            e.cas_unique = casCounter++; 
     196 
    159197            this.cacheStorage.put(e.keystring, e); // save new value 
    160198            return old_val; 
     
    345383        return getMisses; 
    346384    } 
     385 
    347386}