open
pub fn open(addr string) Client用途
打开资源或建立网络连接。失败时抛出错误,调用方需处理或继续传播。
参数
addrstring- 地址(string)
返回值
Client — 该类型的值
#github.com/xulang-org/packages/memcached扩展包 `memcached #github.com/xulang-org/packages/memcached`,由模块 github.com/xulang-org/packages 提供。
github.com/xulang-org/packages · 1.17.2 · 安装与使用
以下源码来自官方 examples 仓库,可复制或下载。请在完整工程中使用本地 Xu SDK 运行;网络、数据库和桌面示例需要对应的服务或资源。
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) Client打开资源或建立网络连接。失败时抛出错误,调用方需处理或继续传播。
addr stringClient — 该类型的值
struct Clientpub struct Client {
pub fd int
pub buf string
pub addr string
}Memcached 客户端会话实例,维护与服务端的连接并提供操作接口。
fd intbuf stringaddr stringdialpub fn dial(addr string) Client发起连接。失败时抛出错误,调用方需处理或继续传播。
addr stringClient — 该类型的值
($&Client) read_linepub fn (c $&Client) read_line() string$&Client 上的操作:执行 read line 操作。失败时抛出错误,调用方需处理或继续传播。
c $&ClientClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。string — 结果字符串
($&Client) read_bytespub fn (c $&Client) read_bytes(n int) string$&Client 上的操作:读到 delim(含 delim)。失败时抛出错误,调用方需处理或继续传播。
c $&ClientClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。n intstring — 结果字符串
($&Client) write_rawpub fn (c $&Client) write_raw(data string) int$&Client 上的操作:执行 write raw 操作。失败时抛出错误,调用方需处理或继续传播。
c $&ClientClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。data stringint — 该类型的值
($&Client) closepub fn (c $&Client) close() int$&Client 上的操作:关闭资源。
c $&ClientClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。int — 该类型的值
($&Client) setpub fn (c $&Client) set(key string, val string, flags int, exptime int) bool在 $&Client 上执行 set 操作。失败时抛出错误,调用方需处理或继续传播。
c $&ClientClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。key stringval stringflags intexptime intbool — 是否成立
($&Client) addpub fn (c $&Client) add(key string, val string, flags int, exptime int) bool$&Client 上的操作:相加。失败时抛出错误,调用方需处理或继续传播。
c $&ClientClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。key stringval stringflags intexptime intbool — 是否成立
($&Client) replacepub fn (c $&Client) replace(key string, val string, flags int, exptime int) bool$&Client 上的操作:替换子串,次数由 n 限制。失败时抛出错误,调用方需处理或继续传播。
c $&ClientClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。key stringval stringflags intexptime intbool — 是否成立
($&Client) appendpub fn (c $&Client) append(key string, val string) bool$&Client 上的操作:追加。失败时抛出错误,调用方需处理或继续传播。
c $&ClientClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。key stringval stringbool — 是否成立
($&Client) prependpub fn (c $&Client) prepend(key string, val string) bool在 $&Client 上执行 prepend 操作。失败时抛出错误,调用方需处理或继续传播。
c $&ClientClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。key stringval stringbool — 是否成立
($&Client) getpub fn (c $&Client) get(key string) string在 $&Client 上执行 get 操作。失败时抛出错误,调用方需处理或继续传播。
c $&ClientClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。key stringstring — 结果字符串
($&Client) get_multipub fn (c $&Client) get_multi(keys []string) []string$&Client 上的操作:读取 multi。失败时抛出错误,调用方需处理或继续传播。
c $&ClientClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。keys []string[]string — 切片结果
($&Client) deletepub fn (c $&Client) delete(key string) bool$&Client 上的操作:删除。失败时抛出错误,调用方需处理或继续传播。
c $&ClientClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。key stringbool — 是否成立
($&Client) incrpub fn (c $&Client) incr(key string, delta int) int在 $&Client 上执行 incr 操作。失败时抛出错误,调用方需处理或继续传播。
c $&ClientClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。key stringdelta intint — 该类型的值
($&Client) decrpub fn (c $&Client) decr(key string, delta int) int在 $&Client 上执行 decr 操作。失败时抛出错误,调用方需处理或继续传播。
c $&ClientClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。key stringdelta intint — 该类型的值
($&Client) touchpub fn (c $&Client) touch(key string, exptime int) bool在 $&Client 上执行 touch 操作。失败时抛出错误,调用方需处理或继续传播。
c $&ClientClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。key stringexptime intbool — 是否成立
($&Client) versionpub fn (c $&Client) version() string在 $&Client 上执行 version 操作。失败时抛出错误,调用方需处理或继续传播。
c $&ClientClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。string — 结果字符串
($&Client) flush_allpub fn (c $&Client) flush_all() bool$&Client 上的操作:执行 flush all 操作。失败时抛出错误,调用方需处理或继续传播。
c $&ClientClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。bool — 是否成立
($&Client) statspub fn (c $&Client) stats() []string在 $&Client 上执行 stats 操作。失败时抛出错误,调用方需处理或继续传播。
c $&ClientClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。[]string — 切片结果
struct Poolpub struct Pool {
pub addr string
pub idle chan int
pub max_idle int
}Memcached 连接池管理器,并发安全地维护和调度复用套接字连接。
addr stringidle chan intmax_idle intnew_poolpub fn new_pool(addr string, max_idle int) Pool构造 pool。
addr stringmax_idle intPool — 该类型的值
(&Pool) getpub fn (p &Pool) get() Client在 &Pool 上执行 get 操作。失败时抛出错误,调用方需处理或继续传播。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。Client — 该类型的值
(&Pool) putpub fn (p &Pool) put(c Client)写入或更新指定键对应的值数据。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。c Client不返回值。
(&Pool) closepub fn (p &Pool) close()&Pool 上的操作:关闭资源。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。不返回值。
struct TlsClientpub struct TlsClient {
pub conn transport.Conn
pub host string
pub port int
}Memcached 客户端会话实例,维护与服务端的连接并提供操作接口。
conn transport.Connhost stringport inttls_connectpub fn tls_connect(endpoint string, default_port int) TlsClient执行 tls connect 操作。失败时抛出错误,调用方需处理或继续传播。
endpoint stringdefault_port intTlsClient — 该类型的值
tls_connect_configpub fn tls_connect_config(endpoint string, default_port int, cfg tls.Config) TlsClient执行 tls connect config 操作。失败时抛出错误,调用方需处理或继续传播。
endpoint stringdefault_port intcfg tls.ConfigTlsClient — 该类型的值
($&TlsClient) write_allpub fn (c $&TlsClient) write_all(data string) int$&TlsClient 上的操作:执行 write all 操作。失败时抛出错误,调用方需处理或继续传播。
c $&TlsClientTlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。data stringint — 该类型的值
($&TlsClient) readpub fn (c $&TlsClient) read(max int) string$&TlsClient 上的操作:读出最多 max 字节。失败时抛出错误,调用方需处理或继续传播。
c $&TlsClientTlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。max intstring — 结果字符串
($&TlsClient) read_exactpub fn (c $&TlsClient) read_exact(need int) string$&TlsClient 上的操作:执行 read exact 操作。失败时抛出错误,调用方需处理或继续传播。
c $&TlsClientTlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。need intstring — 结果字符串
($&TlsClient) closepub fn (c $&TlsClient) close() int$&TlsClient 上的操作:关闭资源。失败时抛出错误,调用方需处理或继续传播。
c $&TlsClientTlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。int — 该类型的值
($&TlsClient) set_deadlinepub fn (c $&TlsClient) set_deadline(t_ns int) int$&TlsClient 上的操作:设置 deadline。失败时抛出错误,调用方需处理或继续传播。
c $&TlsClientTlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。t_ns intint — 该类型的值
($&TlsClient) set_read_deadlinepub fn (c $&TlsClient) set_read_deadline(t_ns int) int$&TlsClient 上的操作:设置 read deadline。失败时抛出错误,调用方需处理或继续传播。
c $&TlsClientTlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。t_ns intint — 该类型的值
($&TlsClient) set_write_deadlinepub fn (c $&TlsClient) set_write_deadline(t_ns int) int$&TlsClient 上的操作:设置 write deadline。失败时抛出错误,调用方需处理或继续传播。
c $&TlsClientTlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。t_ns intint — 该类型的值