9.3.10. Пример интеграции RobotNET Cache

sdk\c-sharp\cache\cache\Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;

using RobotNET.SDK;

namespace RoboCache
{
    class Program
    {
        static void Main( string[] args )
        {
            Console.WriteLine( string.Format( "Program started!" ) );

            if( args.Count() > 1 )
            {
                string addr = args[0];
                string pass = args[1];

                bool lifetime = false;
                bool pending = false;
                bool stackfifo = false;
                bool stacklifo = false;
                bool batchlist = false;
                bool batchget = false;

                int i = 2, j = 0;
                bool next = true;
                for( i = 2; i < args.Count() && next; i++ )
                {
                    next = false;
                    j = i;

                    if( args[i] == "lifetime" ) next = lifetime = true;
                    if( args[i] == "pending" ) next = pending = true;
                    if( args[i] == "fifo" ) next = stackfifo = true;
                    if( args[i] == "lifo" ) next = stacklifo = true;
                    if( args[i] == "batch-list" ) next = batchlist = true;
                    if( args[i] == "batch-get" ) next = batchget = true;
                }

                string key = args.Count() > j ? args[j] : null;
                string val = args.Count() > j + 1 ? args[j + 1] : null;

                string result = string.Empty;

                try
                {
                    using( Cache cache = new Cache( addr, pass, uint.MaxValue ) )
                    {
                        byte[] buffer;

                        if( batchlist )
                        {
                            result = cache.BatchList( key, 10000 );
                        } else
                        if( batchget )
                        {
                            Dictionary<string, byte[]> get = cache.BatchGet( key, 10000 );
                            foreach( var v in get )
                            {
                                var r = string.Join( ",", v.Value.Select( p => p.ToString() ).ToArray() );
                                result += string.Format( "{0}={1}\n", v.Key, r );
                            }
                        } else
                        {
                            if( val != null )
                            {
                                int err;
                                buffer = Encoding.Unicode.GetBytes( val );
                                if( stackfifo )
                                    err = pending ? cache.QueuePending( key, buffer, ( uint )( lifetime ? 60000 : 0 ), 45000 ) : cache.Queue( key, buffer, ( uint )( lifetime ? 60000 : 0 ), 45000 );
                                else
                                    if( stacklifo )
                                        err = pending ? cache.PushPending( key, buffer, ( uint )( lifetime ? 60000 : 0 ), 45000 ) : cache.Push( key, buffer, ( uint )( lifetime ? 60000 : 0 ), 45000 );
                                    else
                                        err = pending ? cache.SetPending( key, buffer, ( uint )( lifetime ? 60000 : 0 ), 45000 ) : cache.Set( key, buffer, ( uint )( lifetime ? 60000 : 0 ), 45000 );
                            }
                            else
                            {
                                if( stackfifo )
                                    buffer = pending ? cache.DequeuePending( key, 60000 ) : cache.Dequeue( key, 45000 );
                                else
                                    if( stacklifo )
                                        buffer = pending ? cache.PopPending( key, 60000 ) : cache.Pop( key, 45000 );
                                    else
                                        buffer = pending ? cache.GetPending( key, 60000 ) : cache.Get( key, 45000 );

                                if( buffer != null )
                                    result = Encoding.Unicode.GetString( buffer, 0, buffer.Length );
                            }
                        }
                    }
                    Console.WriteLine( result );
                }
                catch( Exception e )
                {
                    Console.WriteLine( e.ToString() );
                }
            }

            Console.WriteLine( string.Format( "Program finished!" ) );
        }
    }
}