open
pub fn open(addr string) ClientPurpose
Open resource or establish connection. Throws on failure; handle the error or propagate it to the caller.
Parameters
addrstring- address (string)
Returns
Client — a value of this type
#github.com/xulang-org/packages/memcachedMemcached client package for Xu. Features Standard Memcached Text Protocol implementation in 100% pure Xu. High-performance socket line & byte buffering. Full command suite: set, add, replace, append, prepend, get, delete, incr, decr, touch, stats, flush_all. Channel-based concurrency-safe connection pooling.
github.com/xulang-org/packages · 1.17.2 · Install and use
These sources come from the official examples repository and can be copied or downloaded. Run them with a local Xu SDK in the complete project; network, database, and desktop examples require the corresponding services or assets.
23_standalone_memcached/main.xu@main
memcached #github.com/xulang-org/packages/memcached
#os
fn main() int {
println("=== Memcached Standalone Library Integration Test ===")
addr := "127.0.0.1:11211"
println("Connecting to Memcached at " + addr + "...")
$mc := memcached.open(addr + "") >>> (err) {
println("Connection to Memcached failed (skipping live tests): " + err.msg())
<< 0
}
ver := mc.version() >>> (err) {
println("Version failed: " + err.msg())
<< 1
}
println("1. Memcached version: " + ver)
// Set & Get
set_ok := mc.set("xu_test_key", "Hello Memcached from Xu!", 0, 300) >>> (err) {
println("Set failed: " + err.msg())
<< 1
}
println("2. SET xu_test_key ok=" + str(set_ok))
val := mc.get("xu_test_key") >>> (err) {
println("Get failed: " + err.msg())
<< 1
}
println("3. GET xu_test_key: " + val)
// Incr & Decr
mc.set("xu_counter", "100", 0, 300) >>> (err) {
<< 1
}
new_n := mc.incr("xu_counter", 5) >>> (err) {
println("Incr failed: " + err.msg())
<< 1
}
println("4. INCR xu_counter by 5 = " + str(new_n))
decr_n := mc.decr("xu_counter", 2) >>> (err) {
println("Decr failed: " + err.msg())
<< 1
}
println("5. DECR xu_counter by 2 = " + str(decr_n))
// Append & Prepend
mc.set("xu_str", "World", 0, 300) >>> (err) {
<< 1
}
mc.prepend("xu_str", "Hello ") >>> (err) {
<< 1
}
mc.append("xu_str", "!") >>> (err) {
<< 1
}
prep_val := mc.get("xu_str") >>> (err) {
<< 1
}
println("6. Prepend & Append result: " + prep_val)
// Delete
del_ok := mc.delete("xu_test_key") >>> (err) {
<< 1
}
println("7. DELETE xu_test_key ok=" + str(del_ok))
mc.close()
// Test Pool with Reuse & Concurrency
pool := memcached.new_pool(addr + "", 5)
$p_conn := pool.get() >>> (err) {
println("Pool get failed: " + err.msg())
<< 1
}
fd_first := p_conn.fd
p_ver := p_conn.version() >>> (err) {
<< 1
}
println("8. Pool connection version: " + p_ver + " (fd=" + str(fd_first) + ")")
pool.put(p_conn)
println("9. Pool connection returned successfully.")
// Verify Pool Connection REUSE: second get gives same fd
$p_conn2 := pool.get() >>> (err) {
println("Pool second get failed: " + err.msg())
<< 1
}
fd_second := p_conn2.fd
if fd_first == fd_second {
println("10. [PASS] Pool Connection REUSE verified! (fd_first == fd_second: " + str(fd_first) + ")")
} else {
println("10. [WARN] Pool connection not reused: fd1=" + str(fd_first) + ", fd2=" + str(fd_second))
}
// Concurrent borrow another connection
$p_conn3 := pool.get() >>> (err) {
println("Pool third get failed: " + err.msg())
<< 1
}
fd_third := p_conn3.fd
if fd_second != fd_third {
println("11. [PASS] Pool Multi-connection concurrency verified! (fd2=" + str(fd_second) + " != fd3=" + str(fd_third) + ")")
}
p_conn3.set("pool_mc_key", "mc_val", 0, 300) >>> (err) {
<< 1
}
got_mc := p_conn2.get("pool_mc_key") >>> (err) {
<< 1
}
println("12. [PASS] Concurrent connection cross-get verified: got=" + got_mc)
pool.put(p_conn2)
pool.put(p_conn3)
pool.close()
println("13. [PASS] Memcached Pool closed cleanly.")
println("=== Memcached Tests Completed Successfully! ===")
<< 0
}openpub fn open(addr string) ClientOpen resource or establish connection. Throws on failure; handle the error or propagate it to the caller.
addr stringClient — a value of this type
struct Clientpub struct Client {
pub fd int
pub buf string
pub addr string
}Memcached Client session maintaining connection and operations.
fd intbuf stringaddr stringdialpub fn dial(addr string) ClientDial a connection. Throws on failure; handle the error or propagate it to the caller.
addr stringClient — a value of this type
($&Client) read_linepub fn (c $&Client) read_line() stringOperation on $&Client: read line. Throws on failure; handle the error or propagate it to the caller.
c $&ClientClient instance used by this call (exclusive borrow).string — result string
($&Client) read_bytespub fn (c $&Client) read_bytes(n int) stringOperation on $&Client: read through delim (inclusive). Throws on failure; handle the error or propagate it to the caller.
c $&ClientClient instance used by this call (exclusive borrow).n intstring — result string
($&Client) write_rawpub fn (c $&Client) write_raw(data string) intOperation on $&Client: write raw. Throws on failure; handle the error or propagate it to the caller.
c $&ClientClient instance used by this call (exclusive borrow).data stringint — a value of this type
($&Client) closepub fn (c $&Client) close() intOperation on $&Client: close the resource.
c $&ClientClient instance used by this call (exclusive borrow).int — a value of this type
($&Client) setpub fn (c $&Client) set(key string, val string, flags int, exptime int) boolExecute set on $&Client. Throws on failure; handle the error or propagate it to the caller.
c $&ClientClient instance used by this call (exclusive borrow).key stringval stringflags intexptime intbool — true or false
($&Client) addpub fn (c $&Client) add(key string, val string, flags int, exptime int) boolOperation on $&Client: add. Throws on failure; handle the error or propagate it to the caller.
c $&ClientClient instance used by this call (exclusive borrow).key stringval stringflags intexptime intbool — true or false
($&Client) replacepub fn (c $&Client) replace(key string, val string, flags int, exptime int) boolOperation on $&Client: replace a substring, limited by n. Throws on failure; handle the error or propagate it to the caller.
c $&ClientClient instance used by this call (exclusive borrow).key stringval stringflags intexptime intbool — true or false
($&Client) appendpub fn (c $&Client) append(key string, val string) boolOperation on $&Client: append. Throws on failure; handle the error or propagate it to the caller.
c $&ClientClient instance used by this call (exclusive borrow).key stringval stringbool — true or false
($&Client) prependpub fn (c $&Client) prepend(key string, val string) boolExecute prepend on $&Client. Throws on failure; handle the error or propagate it to the caller.
c $&ClientClient instance used by this call (exclusive borrow).key stringval stringbool — true or false
($&Client) getpub fn (c $&Client) get(key string) stringExecute get on $&Client. Throws on failure; handle the error or propagate it to the caller.
c $&ClientClient instance used by this call (exclusive borrow).key stringstring — result string
($&Client) get_multipub fn (c $&Client) get_multi(keys []string) []stringOperation on $&Client: read multi. Throws on failure; handle the error or propagate it to the caller.
c $&ClientClient instance used by this call (exclusive borrow).keys []string[]string — slice result
($&Client) deletepub fn (c $&Client) delete(key string) boolExecute delete on $&Client. Throws on failure; handle the error or propagate it to the caller.
c $&ClientClient instance used by this call (exclusive borrow).key stringbool — true or false
($&Client) incrpub fn (c $&Client) incr(key string, delta int) intExecute incr on $&Client. Throws on failure; handle the error or propagate it to the caller.
c $&ClientClient instance used by this call (exclusive borrow).key stringdelta intint — a value of this type
($&Client) decrpub fn (c $&Client) decr(key string, delta int) intExecute decr on $&Client. Throws on failure; handle the error or propagate it to the caller.
c $&ClientClient instance used by this call (exclusive borrow).key stringdelta intint — a value of this type
($&Client) touchpub fn (c $&Client) touch(key string, exptime int) boolExecute touch on $&Client. Throws on failure; handle the error or propagate it to the caller.
c $&ClientClient instance used by this call (exclusive borrow).key stringexptime intbool — true or false
($&Client) versionpub fn (c $&Client) version() stringExecute version on $&Client. Throws on failure; handle the error or propagate it to the caller.
c $&ClientClient instance used by this call (exclusive borrow).string — result string
($&Client) flush_allpub fn (c $&Client) flush_all() boolOperation on $&Client: flush all. Throws on failure; handle the error or propagate it to the caller.
c $&ClientClient instance used by this call (exclusive borrow).bool — true or false
($&Client) statspub fn (c $&Client) stats() []stringExecute stats on $&Client. Throws on failure; handle the error or propagate it to the caller.
c $&ClientClient instance used by this call (exclusive borrow).[]string — slice result
struct Poolpub struct Pool {
pub addr string
pub idle chan int
pub max_idle int
}Memcached Connection pool manager scheduling reusable socket connections.
addr stringidle chan intmax_idle intnew_poolpub fn new_pool(addr string, max_idle int) PoolConstruct pool.
addr stringmax_idle intPool — a value of this type
(&Pool) getpub fn (p &Pool) get() ClientExecute get on &Pool. Throws on failure; handle the error or propagate it to the caller.
p &PoolPool instance used by this call (borrow).Client — a value of this type
(&Pool) putpub fn (p &Pool) put(c Client)Store or update the value associated with the specified key.
p &PoolPool instance used by this call (borrow).c ClientThis call does not return a value.
(&Pool) closepub fn (p &Pool) close()Operation on &Pool: close the resource.
p &PoolPool instance used by this call (borrow).This call does not return a value.
struct TlsClientpub struct TlsClient {
pub conn transport.Conn
pub host string
pub port int
}Memcached Client session maintaining connection and operations.
conn transport.Connhost stringport inttls_connectpub fn tls_connect(endpoint string, default_port int) TlsClientTls connect. Throws on failure; handle the error or propagate it to the caller.
endpoint stringdefault_port intTlsClient — a value of this type
tls_connect_configpub fn tls_connect_config(endpoint string, default_port int, cfg tls.Config) TlsClientTls connect config. Throws on failure; handle the error or propagate it to the caller.
endpoint stringdefault_port intcfg tls.ConfigTlsClient — a value of this type
($&TlsClient) write_allpub fn (c $&TlsClient) write_all(data string) intOperation on $&TlsClient: write all. Throws on failure; handle the error or propagate it to the caller.
c $&TlsClientTlsClient instance used by this call (exclusive borrow).data stringint — a value of this type
($&TlsClient) readpub fn (c $&TlsClient) read(max int) stringOperation on $&TlsClient: read up to max bytes. Throws on failure; handle the error or propagate it to the caller.
c $&TlsClientTlsClient instance used by this call (exclusive borrow).max intstring — result string
($&TlsClient) read_exactpub fn (c $&TlsClient) read_exact(need int) stringOperation on $&TlsClient: read exact. Throws on failure; handle the error or propagate it to the caller.
c $&TlsClientTlsClient instance used by this call (exclusive borrow).need intstring — result string
($&TlsClient) closepub fn (c $&TlsClient) close() intOperation on $&TlsClient: close the resource. Throws on failure; handle the error or propagate it to the caller.
c $&TlsClientTlsClient instance used by this call (exclusive borrow).int — a value of this type
($&TlsClient) set_deadlinepub fn (c $&TlsClient) set_deadline(t_ns int) intOperation on $&TlsClient: set deadline. Throws on failure; handle the error or propagate it to the caller.
c $&TlsClientTlsClient instance used by this call (exclusive borrow).t_ns intint — a value of this type
($&TlsClient) set_read_deadlinepub fn (c $&TlsClient) set_read_deadline(t_ns int) intOperation on $&TlsClient: set read deadline. Throws on failure; handle the error or propagate it to the caller.
c $&TlsClientTlsClient instance used by this call (exclusive borrow).t_ns intint — a value of this type
($&TlsClient) set_write_deadlinepub fn (c $&TlsClient) set_write_deadline(t_ns int) intOperation on $&TlsClient: set write deadline. Throws on failure; handle the error or propagate it to the caller.
c $&TlsClientTlsClient instance used by this call (exclusive borrow).t_ns intint — a value of this type