Category: memcached

  • Khóa học miễn phí Memcached – Delete Key nhận dự án làm có lương

    Memcached – Delete Key



    Memcached delete command is used to delete an existing key from the Memcached server.

    Syntax

    The basic syntax of Memcached delete command is as shown below −

    delete key [noreply]
    

    Output

    CAS command may produce one of the following result −

    • DELETED indicates successful deletion.

    • ERROR indicates error while deleting data or wrong syntax.

    • NOT_FOUND indicates that the key does not exist in the Memcached server.

    Example

    In this example, we use tutorialspoint as a key and store memcached in it with an expiration time of 900 seconds. After this, it deletes the stored key.

    set tutorialspoint 0 900 9
    memcached
    STORED
    get tutorialspoint
    VALUE tutorialspoint 0 9
    memcached
    END
    delete tutorialspoint
    DELETED
    get tutorialspoint
    END
    delete tutorialspoint
    NOT_FOUND
    

    Delete Data Using Java Application

    To delete data from a Memcached server, you need to use the Memcached delete method.

    Example

    import java.net.InetSocketAddress;
    import java.util.concurrent.Future;
    
    import net.spy.memcached.MemcachedClient;
    
    public class MemcachedJava {
       public static void main(String[] args) {
    
          try{
    
             // Connecting to Memcached server on localhost
             MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
             System.out.println("Connection to server sucessful.");
    
             // add data to memcached server
             Future fo = mcc.set("tutorialspoint", 900, "World''s largest online tutorials library");
    
             // print status of set method
             System.out.println("set status:" + fo.get());
    
             // retrieve and check the value from cache
             System.out.println("tutorialspoint value in cache - " + mcc.get("tutorialspoint"));
    
             // try to add data with existing key
             Future fo = mcc.delete("tutorialspoint");
    
             // print status of delete method
             System.out.println("delete status:" + fo.get());
    
             // retrieve and check the value from cache
             System.out.println("tutorialspoint value in cache - " + mcc.get("codingground"));
    
             // Shutdowns the memcached client
             mcc.shutdown();
    
          }catch(Exception ex)
             System.out.println(ex.getMessage());
       }
    }
    

    Output

    On compiling and executing the program, you get to see the following output −

    Connection to server successful
    set status:true
    tutorialspoint value in cache - World''s largest online tutorials library
    delete status:true
    tutorialspoint value in cache - null
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí Memcached – Incr/Decr nhận dự án làm có lương

    Memcached – Increment Decrement Data



    Memcached incr and decr commands are used to increment or decrement the numeric value of an existing key. If the key is not found, then it returns NOT_FOUND. If the key is not numeric, then it returns CLIENT_ERROR cannot increment or decrement non-numeric value. Otherwise, ERROR is returned.

    Syntax – incr

    The basic syntax of Memcached incr command is as shown below −

    incr key increment_value
    

    Example

    In this example, we use visitors as key and set 10 initially into it, thereafter we increment the visitors by 5.

    set visitors 0 900 2
    10
    STORED
    get visitors
    VALUE visitors 0 2
    10
    END
    incr visitors 5
    15
    get visitors
    VALUE visitors 0 2
    15
    END
    

    Syntax – decr

    The basic syntax of Memcached decr command is as shown below

    decr key decrement_value
    

    Example

    set visitors 0 900 2
    10
    STORED
    get visitors
    VALUE visitors 0 2
    10
    END
    decr visitors 5
    5
    get visitors
    VALUE visitors 0 1
    5
    END
    

    Incr/Decr Using Java Application

    To increment or decrement data in a Memcached server, you need to use Memcached incr or decr methods respectively.

    Example

    import net.spy.memcached.MemcachedClient;
    public class MemcachedJava {
       public static void main(String[] args) {
    
          // Connecting to Memcached server on localhost
          MemcachedClient mcc = new MemcachedClient(new
          InetSocketAddress("127.0.0.1", 11211));
          System.out.println("Connection to server sucessfully");
          System.out.println("set status:"+mcc.set("count", 900, "5").isDone());
    
          // Get value from cache
          System.out.println("Get from Cache:"+mcc.get("count"));
    
          // now increase the stored value
          System.out.println("Increment value:"+mcc.incr("count", 2));
    
          // now decrease the stored value
          System.out.println("Decrement value:"+mcc.decr("count", 1));
    
          // now get the final stored value
          System.out.println("Get from Cache:"+mcc.get("count"));
       }
    }
    

    Output

    On compiling and executing the program, you get to see the following output −

    Connection to server successfully
    set status:true
    Get from Cache:5
    Increment value:7
    Decrement value:6
    Get from Cache:6
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí Memcached – Stats Items nhận dự án làm có lương

    Memcached – Stats Items



    Memcached stats items command is used to get items statistics such as count, age, eviction, etc. organized by slabs ID.

    Syntax

    The basic syntax of Memcached stats items command is as shown below −

    stats items
    

    Example

    stats items
    STAT items:1:number 1
    STAT items:1:age 7
    STAT items:1:evicted 0
    STAT items:1:evicted_nonzero 0
    STAT items:1:evicted_time 0
    STAT items:1:outofmemory 0
    STAT items:1:tailrepairs 0
    STAT items:1:reclaimed 0
    STAT items:1:expired_unfetched 0
    STAT items:1:evicted_unfetched 0
    END
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí Memcached – Delete Data nhận dự án làm có lương

    Memcached – Delete data



    Memcached delete command is used to delete an existing key from the Memcached server.

    Syntax

    The basic syntax of Memcached delete command is as shown below −

    delete key
    

    If the key is successfully deleted, then it returns DELETED. If the key is not found, then it returns NOT_FOUND, otherwise it returns ERROR.

    Example

    In this example, we use tutorialspoint as a key and store memcached in it with an expiration time of 900 seconds. After this, it deletes the stored key.

    set tutorialspoint 0 900 9
    memcached
    STORED
    get tutorialspoint
    VALUE tutorialspoint 0 9
    memcached
    END
    delete tutorialspoint
    DELETED
    get tutorialspoint
    END
    delete tutorialspoint
    NOT_FOUND
    

    Delete Data Using Java Application

    To delete data from a Memcached server, you need to use the Memcached delete method.

    Example

    import net.spy.memcached.MemcachedClient;
    public class MemcachedJava {
       public static void main(String[] args) {
    
          // Connecting to Memcached server on localhost
          MemcachedClient mcc = new MemcachedClient(new
          InetSocketAddress("127.0.0.1", 11211));
          System.out.println("Connection to server successful");
          System.out.println("set status:"+mcc.set("tutorialspoint", 900, "memcached").done);
    
          // Get value from cache
          System.out.println("Get from Cache:"+mcc.get("tutorialspoint"));
    
          // delete value from cache
          System.out.println("Delete from Cache:"+mcc.delete("tutorialspoint").isDone());
    
          // check whether value exists or not
          System.out.println("Get from Cache:"+mcc.get("tutorialspoint"));
       }
    }
    

    Output

    On compiling and executing the program, you get to see the following output −

    Connection to server successful
    set status:true
    Get from Cache:memcached
    Delete from Cache:true
    Get from Cache:null
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí Memcached – Get Data nhận dự án làm có lương

    Memcached – Get Data



    Memcached get command is used to get the value stored at key. If the key does not exist in Memcached, then it returns nothing.

    Syntax

    The basic syntax of Memcached get command is as shown below −

    get key
    

    Example

    In the following example, we use tutorialspoint as the key and store memcached in it with an expiration time of 900 seconds.

    set tutorialspoint 0 900 9
    memcached
    STORED
    get tutorialspoint
    VALUE tutorialspoint 0 9
    memcached
    END
    

    Get Data Using Java Application

    To get data from a Memcached server, you need to use the Memcached get method.

    Example

    import net.spy.memcached.MemcachedClient;
    public class MemcachedJava {
       public static void main(String[] args) {
          // Connecting to Memcached server on localhost
          MemcachedClient mcc = new MemcachedClient(new
          InetSocketAddress("127.0.0.1", 11211));
          System.out.println("Connection to server sucessfully");
          System.out.println("set status:"+mcc.set("tutorialspoint", 900, "memcached").done);
    
         // Get value from cache
          System.out.println("Get from Cache:"+mcc.get("tutorialspoint"));
       }
    }
    

    Output

    On compiling and executing the program, you get to see the following output −

    Connection to server successfully
    set status:true
    Get from Cache:memcached
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí Memcached – Stats Slabs nhận dự án làm có lương

    Memcached – Stats Slabs



    Memcached stats slabs command displays slabs statistics such as size, memory usage, commands, count etc. organized by slabs ID.

    Syntax

    The basic syntax of Memcached stats slabs command is as shown below −

    stats slabs
    

    Example

    stats slabs
    STAT 1:chunk_size 96
    STAT 1:chunks_per_page 10922
    STAT 1:total_pages 1
    STAT 1:total_chunks 10922
    STAT 1:used_chunks 1
    STAT 1:free_chunks 10921
    STAT 1:free_chunks_end 0
    STAT 1:mem_requested 71
    STAT 1:get_hits 0
    STAT 1:cmd_set 1
    STAT 1:delete_hits 0
    STAT 1:incr_hits 0
    STAT 1:decr_hits 0
    STAT 1:cas_hits 0
    STAT 1:cas_badval 0
    STAT 1:touch_hits 0
    STAT active_slabs 1
    STAT total_malloced 1048512
    END
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí Memcached – Get CAS Data nhận dự án làm có lương

    Memcached – Get CAS Data



    Memcached gets command is used to get the value with CAS token. If the key does not exist in Memcached, then it returns nothing.

    Syntax

    The basic syntax of Memcached gets command is as shown below −

    get key
    

    Example

    set tutorialspoint 0 900 9
    memcached
    STORED
    gets tutorialspoint
    VALUE tutorialspoint 0 9 1
    memcached
    END
    

    In this example, we use tutorialspoint as the key and store memcached in it with an expiration time of 900 seconds.

    Get CAS Data Using Java Application

    To get CAS data from a Memcached server, you need to use the Memcached gets method.

    Example

    import net.spy.memcached.MemcachedClient;
    public class MemcachedJava {
       public static void main(String[] args) {
    
          // Connecting to Memcached server on localhost
          MemcachedClient mcc = new MemcachedClient(new
          InetSocketAddress("127.0.0.1", 11211));
          System.out.println("Connection to server sucessfully");
          System.out.println("set status:"+mcc.set("tutorialspoint", 900, "memcached").done);
    
          // Get value from cache
          System.out.println("Get from Cache:"+mcc.gets("tutorialspoint"));
       }
    }
    

    Output

    On compiling and executing the program, you get to see the following output −

    Connection to server successfully
    set status:true
    Get from Cache:{CasValue 2/memcached}
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí Memcached – Stats nhận dự án làm có lương

    Memcached – Stats



    Memcached stats command is used to return server statistics such as PID, version, connections, etc.

    Syntax

    The basic syntax of Memcached stats command is as shown below −

    stats
    

    Example

    stats
    STAT pid 1162
    STAT uptime 5022
    STAT time 1415208270
    STAT version 1.4.14
    STAT libevent 2.0.19-stable
    STAT pointer_size 64
    STAT rusage_user 0.096006
    STAT rusage_system 0.152009
    STAT curr_connections 5
    STAT total_connections 6
    
    STAT connection_structures 6
    STAT reserved_fds 20
    STAT cmd_get 6
    STAT cmd_set 4
    STAT cmd_flush 0
    STAT cmd_touch 0
    STAT get_hits 4
    STAT get_misses 2
    STAT delete_misses 1
    STAT delete_hits 1
    
    STAT incr_misses 2
    STAT incr_hits 1
    STAT decr_misses 0
    STAT decr_hits 1
    STAT cas_misses 0
    STAT cas_hits 0
    STAT cas_badval 0
    STAT touch_hits 0
    STAT touch_misses 0
    STAT auth_cmds 0
    
    STAT auth_errors 0
    STAT bytes_read 262
    STAT bytes_written 313
    STAT limit_maxbytes 67108864
    STAT accepting_conns 1
    STAT listen_disabled_num 0
    STAT threads 4
    STAT conn_yields 0
    STAT hash_power_level 16
    
    STAT hash_bytes 524288
    STAT hash_is_expanding 0
    STAT expired_unfetched 1
    STAT evicted_unfetched 0
    STAT bytes 142
    STAT curr_items 2
    STAT total_items 6
    STAT evictions 0
    STAT reclaimed 1
    END
    

    Stats Using Java Application

    To get stats from a Memcached server, you need to use the Memcached stats method.

    Example

    import net.spy.memcached.MemcachedClient;
    public class MemcachedJava {
       public static void main(String[] args) {
    
          // Connecting to Memcached server on localhost
          MemcachedClient mcc = new MemcachedClient(new
          InetSocketAddress("127.0.0.1", 11211));
          System.out.println("Connection to server successful");
          System.out.println("Stats:"+mcc.stats);
       }
    }
    

    Output

    On compiling and executing the program, you get to see the following output −

    Connection to server successful
    Stats:[/127.0.0.1:11211:[delete_hits:0, bytes:71, total_items:4,
    rusage_system:0.220013, touch_misses:0, cmd_touch:0, listen_disabled_num:0,
    auth_errors:0, evictions:0, version:1.4.14, pointer_size:64, time:1417279366,
    incr_hits:1, threads:4, expired_unfetched:0, limit_maxbytes:67108864,
    hash_is_expanding:0, bytes_read:170, curr_connections:8, get_misses:1,
    reclaimed:0, bytes_written:225, hash_power_level:16, connection_structures:9,
    cas_hits:0, delete_misses:0, total_connections:11, rusage_user:0.356022,
    cmd_flush:0, libevent:2.0.19-stable, uptime:12015, reserved_fds:20,
    touch_hits:0, cas_badval:0, pid:1138, get_hits:2, curr_items:1, cas_misses:0,
    accepting_conns:1, evicted_unfetched:0, cmd_get:3, cmd_set:2, auth_cmds:0,
    incr_misses:1, hash_bytes:524288, decr_misses:1, decr_hits:1, conn_yields:0]]
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí Memcached – Stats sizes nhận dự án làm có lương

    Memcached – Stats Sizes



    Memcached stats sizes command provides information about the sizes and number of items of each size within the cache. The information is returned in two columns. The first column is the size of the item (rounded up to the nearest 32 byte boundary), and the second column is the count of the number of items of that size within the cache.

    Syntax

    The basic syntax of Memcached stats sizes command is as shown below −

    stats sizes
    

    Example

    stats sizes
    STAT 96 1
    END
    

    The item size statistics are useful only to determine the sizes of the objects you are storing. Since the actual memory allocation is relevant only in terms of the chunk size and page size, the information is only useful during a careful debugging or diagnostic session.


    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí Memcached – Clear Data nhận dự án làm có lương

    Memcached – Clear Data



    Memcached flush_all command is used to delete all data (key-value pairs) from the Memcached server. It accepts an optional parameter called time that sets a time after which the Memcached data is to be cleared.

    Syntax

    The basic syntax of Memcached flush_all command is as shown below −

    flush_all [time] [noreply]
    

    The above command always returns OK.

    Example

    In the following example, we store some data into the Memcached server and then clear all the data.

    set tutorialspoint 0 900 9
    memcached
    STORED
    get tutorialspoint
    VALUE tutorialspoint 0 9
    memcached
    END
    flush_all
    OK
    get tutorialspoint
    END
    

    Clear Data Using Java Application

    To clear data from a Memcached server, you need to use the Memcached flush method.

    Example

    import net.spy.memcached.MemcachedClient;
    public class MemcachedJava {
       public static void main(String[] args) {
    
         // Connecting to Memcached server on localhost
          MemcachedClient mcc = new MemcachedClient(new
          InetSocketAddress("127.0.0.1", 11211));
          System.out.println("Connection to server sucessfully");
          System.out.println("set status:"+mcc.set("count", 900, "5").isDone());
    
          // Get value from cache
          System.out.println("Get from Cache:"+mcc.get("count"));
    
          // now increase the stored value
          System.out.println("Increment value:"+mcc.incr("count", 2));
    
          // now decrease the stored value
          System.out.println("Decrement value:"+mcc.decr("count", 1));
    
          // now get the final stored value
          System.out.println("Get from Cache:"+mcc.get("count"));
    
          // now clear all this data
          System.out.println("Clear data:"+mcc.flush().isDone());
       }
    }
    

    Output

    On compiling and executing the program, you get to see the following output −

    Connection to server successfully
    set status:true
    Get from Cache:5
    Increment value:7
    Decrement value:6
    Get from Cache:6
    Clear data:true
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc