XU XUv0.16.0

#github.com/xulang-org/packages/memcached

import: memcached #github.com/xulang-org/packages/memcached

counts: 5 functions, 28 methods, 3 types, 0 constants, 0 method sets

Memcached 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

Examples · API declarations

Examples

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

Required SDK: Xu 0.8.3 · Complete project · Download source

XuLocal SDK example
@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
}

API declarations

open

XuAPI
pub fn open(addr string) Client

Purpose

Open resource or establish connection. Throws on failure; handle the error or propagate it to the caller.

Parameters

addr string
address (string)

Returns

Client — a value of this type

See usage example

struct Client

XuAPI
pub struct Client {
    pub fd int
    pub buf string
    pub addr string
}

Purpose

Memcached Client session maintaining connection and operations.

Public fields

fd int
file descriptor
buf string
buffer
addr string
address

dial

XuAPI
pub fn dial(addr string) Client

Purpose

Dial a connection. Throws on failure; handle the error or propagate it to the caller.

Parameters

addr string
address (string)

Returns

Client — a value of this type

($&Client) read_line

XuAPI
pub fn (c $&Client) read_line() string

Purpose

Operation on $&Client: read line. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&Client
The Client instance used by this call (exclusive borrow).

Returns

string — result string

($&Client) read_bytes

XuAPI
pub fn (c $&Client) read_bytes(n int) string

Purpose

Operation on $&Client: read through delim (inclusive). Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&Client
The Client instance used by this call (exclusive borrow).
n int
count, length, or limit (int)

Returns

string — result string

($&Client) write_raw

XuAPI
pub fn (c $&Client) write_raw(data string) int

Purpose

Operation on $&Client: write raw. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&Client
The Client instance used by this call (exclusive borrow).
data string
data (string)

Returns

int — a value of this type

($&Client) close

XuAPI
pub fn (c $&Client) close() int

Purpose

Operation on $&Client: close the resource.

Parameters

c $&Client
The Client instance used by this call (exclusive borrow).

Returns

int — a value of this type

($&Client) set

XuAPI
pub fn (c $&Client) set(key string, val string, flags int, exptime int) bool

Purpose

Execute set on $&Client. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&Client
The Client instance used by this call (exclusive borrow).
key string
key (string)
val string
value (string)
flags int
flags (int)
exptime int
exptime (int)

Returns

bool — true or false

($&Client) add

XuAPI
pub fn (c $&Client) add(key string, val string, flags int, exptime int) bool

Purpose

Operation on $&Client: add. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&Client
The Client instance used by this call (exclusive borrow).
key string
key (string)
val string
value (string)
flags int
flags (int)
exptime int
exptime (int)

Returns

bool — true or false

($&Client) replace

XuAPI
pub fn (c $&Client) replace(key string, val string, flags int, exptime int) bool

Purpose

Operation on $&Client: replace a substring, limited by n. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&Client
The Client instance used by this call (exclusive borrow).
key string
key (string)
val string
value (string)
flags int
flags (int)
exptime int
exptime (int)

Returns

bool — true or false

($&Client) append

XuAPI
pub fn (c $&Client) append(key string, val string) bool

Purpose

Operation on $&Client: append. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&Client
The Client instance used by this call (exclusive borrow).
key string
key (string)
val string
value (string)

Returns

bool — true or false

($&Client) prepend

XuAPI
pub fn (c $&Client) prepend(key string, val string) bool

Purpose

Execute prepend on $&Client. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&Client
The Client instance used by this call (exclusive borrow).
key string
key (string)
val string
value (string)

Returns

bool — true or false

($&Client) get

XuAPI
pub fn (c $&Client) get(key string) string

Purpose

Execute get on $&Client. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&Client
The Client instance used by this call (exclusive borrow).
key string
key (string)

Returns

string — result string

($&Client) get_multi

XuAPI
pub fn (c $&Client) get_multi(keys []string) []string

Purpose

Operation on $&Client: read multi. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&Client
The Client instance used by this call (exclusive borrow).
keys []string
keys ([]string)

Returns

[]string — slice result

($&Client) delete

XuAPI
pub fn (c $&Client) delete(key string) bool

Purpose

Execute delete on $&Client. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&Client
The Client instance used by this call (exclusive borrow).
key string
key (string)

Returns

bool — true or false

($&Client) incr

XuAPI
pub fn (c $&Client) incr(key string, delta int) int

Purpose

Execute incr on $&Client. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&Client
The Client instance used by this call (exclusive borrow).
key string
key (string)
delta int
delta (int)

Returns

int — a value of this type

($&Client) decr

XuAPI
pub fn (c $&Client) decr(key string, delta int) int

Purpose

Execute decr on $&Client. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&Client
The Client instance used by this call (exclusive borrow).
key string
key (string)
delta int
delta (int)

Returns

int — a value of this type

($&Client) touch

XuAPI
pub fn (c $&Client) touch(key string, exptime int) bool

Purpose

Execute touch on $&Client. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&Client
The Client instance used by this call (exclusive borrow).
key string
key (string)
exptime int
exptime (int)

Returns

bool — true or false

($&Client) version

XuAPI
pub fn (c $&Client) version() string

Purpose

Execute version on $&Client. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&Client
The Client instance used by this call (exclusive borrow).

Returns

string — result string

($&Client) flush_all

XuAPI
pub fn (c $&Client) flush_all() bool

Purpose

Operation on $&Client: flush all. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&Client
The Client instance used by this call (exclusive borrow).

Returns

bool — true or false

($&Client) stats

XuAPI
pub fn (c $&Client) stats() []string

Purpose

Execute stats on $&Client. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&Client
The Client instance used by this call (exclusive borrow).

Returns

[]string — slice result

struct Pool

XuAPI
pub struct Pool {
    pub addr string
    pub idle chan int
    pub max_idle int
}

Purpose

Memcached Connection pool manager scheduling reusable socket connections.

Public fields

addr string
address
idle chan int
field (channel; type chan int)
max_idle int
field (int)

new_pool

XuAPI
pub fn new_pool(addr string, max_idle int) Pool

Purpose

Construct pool.

Parameters

addr string
address (string)
max_idle int
max_idle (int)

Returns

Pool — a value of this type

See usage example

(&Pool) get

XuAPI
pub fn (p &Pool) get() Client

Purpose

Execute get on &Pool. Throws on failure; handle the error or propagate it to the caller.

Parameters

p &Pool
The Pool instance used by this call (borrow).

Returns

Client — a value of this type

(&Pool) put

XuAPI
pub fn (p &Pool) put(c Client)

Purpose

Store or update the value associated with the specified key.

Parameters

p &Pool
The Pool instance used by this call (borrow).
c Client
byte or character code (Client)

Returns

This call does not return a value.

(&Pool) close

XuAPI
pub fn (p &Pool) close()

Purpose

Operation on &Pool: close the resource.

Parameters

p &Pool
The Pool instance used by this call (borrow).

Returns

This call does not return a value.

struct TlsClient

XuAPI
pub struct TlsClient {
    pub conn transport.Conn
    pub host string
    pub port int
}

Purpose

Memcached Client session maintaining connection and operations.

Public fields

conn transport.Conn
field (transport.Conn)
host string
host
port int
port

tls_connect

XuAPI
pub fn tls_connect(endpoint string, default_port int) TlsClient

Purpose

Tls connect. Throws on failure; handle the error or propagate it to the caller.

Parameters

endpoint string
endpoint (string)
default_port int
default_port (int)

Returns

TlsClient — a value of this type

tls_connect_config

XuAPI
pub fn tls_connect_config(endpoint string, default_port int, cfg tls.Config) TlsClient

Purpose

Tls connect config. Throws on failure; handle the error or propagate it to the caller.

Parameters

endpoint string
endpoint (string)
default_port int
default_port (int)
cfg tls.Config
cfg (tls.Config)

Returns

TlsClient — a value of this type

($&TlsClient) write_all

XuAPI
pub fn (c $&TlsClient) write_all(data string) int

Purpose

Operation on $&TlsClient: write all. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&TlsClient
The TlsClient instance used by this call (exclusive borrow).
data string
data (string)

Returns

int — a value of this type

($&TlsClient) read

XuAPI
pub fn (c $&TlsClient) read(max int) string

Purpose

Operation on $&TlsClient: read up to max bytes. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&TlsClient
The TlsClient instance used by this call (exclusive borrow).
max int
maximum (int)

Returns

string — result string

($&TlsClient) read_exact

XuAPI
pub fn (c $&TlsClient) read_exact(need int) string

Purpose

Operation on $&TlsClient: read exact. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&TlsClient
The TlsClient instance used by this call (exclusive borrow).
need int
need (int)

Returns

string — result string

($&TlsClient) close

XuAPI
pub fn (c $&TlsClient) close() int

Purpose

Operation on $&TlsClient: close the resource. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&TlsClient
The TlsClient instance used by this call (exclusive borrow).

Returns

int — a value of this type

($&TlsClient) set_deadline

XuAPI
pub fn (c $&TlsClient) set_deadline(t_ns int) int

Purpose

Operation on $&TlsClient: set deadline. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&TlsClient
The TlsClient instance used by this call (exclusive borrow).
t_ns int
t_ns (int)

Returns

int — a value of this type

($&TlsClient) set_read_deadline

XuAPI
pub fn (c $&TlsClient) set_read_deadline(t_ns int) int

Purpose

Operation on $&TlsClient: set read deadline. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&TlsClient
The TlsClient instance used by this call (exclusive borrow).
t_ns int
t_ns (int)

Returns

int — a value of this type

($&TlsClient) set_write_deadline

XuAPI
pub fn (c $&TlsClient) set_write_deadline(t_ns int) int

Purpose

Operation on $&TlsClient: set write deadline. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&TlsClient
The TlsClient instance used by this call (exclusive borrow).
t_ns int
t_ns (int)

Returns

int — a value of this type