Memcached class   [3.4.2]

The class is designed for working with memcached servers using libmemcached library.

Example
A tiny user-defined class that works as an operator cache, but the cached data is stored into memcached server:

@main[]
$m[^mcache::open[localhost]]
^m.cache[key2;10]{dt: $d[^date::now[]] ^d.sql-string[] ^sleep(3)}



@CLASS
mcache


@auto[]
$timeout(4) ^rem{
 timeout, seconds }
$retry_on_timeout(false) ^rem{
 retry cache lock attempts }


@open[connect-options]
$m[^memcached::open[$connect-options]]


@cache[key;expires;code][lock;i]
$result[$m.$key]
^if(!def $result){
   ^rem{
 not cached yet }
   $lock[${key}-lock]
   ^while(!^m.add[$lock; $.value[$timeout] $.expires($timeout)]){
      ^rem{
 another process got the lock, waiting ... }
      ^for[i](1;$timeout*5){
         ^sleep(0.2)
         $result[$m.$key]
         ^if(def $result){^break[]}
      }
      ^if(def $result){
         ^break[]
      }{
         ^if(!$retry_on_timeout){
            ^throw[$self.CLASS_NAME;Timeout while getting lock for key '$key']
         }
      }
   }
   ^if(!def $result){
      ^rem{
 we got the lock, processing the code }
      ^try{
         $result[$code]
         $m.[$key][ $.value[$result] $.expires($expires) ]
      }{}{
         ^m.delete[$lock]
      }
   }
}



Copyright © 1997–2021 Art. Lebedev Studio | http://www.artlebedev.com Last updated: 18.06.2012