Interface ScriptingAndFunctionsCommands

  • All Known Implementing Classes:
    GlideClient

    public interface ScriptingAndFunctionsCommands
    Supports commands for the "Scripting and Function" group for a standalone client.
    See Also:
    Scripting and Function Commands
    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      java.util.concurrent.CompletableFuture<java.lang.Object> fcall​(GlideString function)
      Invokes a previously loaded function.
      This command is routed to primary nodes only.
      To route to a replica please refer to fcallReadOnly(java.lang.String).
      java.util.concurrent.CompletableFuture<java.lang.Object> fcall​(java.lang.String function)
      Invokes a previously loaded function.
      This command is routed to primary nodes only.
      To route to a replica please refer to fcallReadOnly(java.lang.String).
      java.util.concurrent.CompletableFuture<java.lang.Object> fcallReadOnly​(GlideString function)
      Invokes a previously loaded read-only function.
      This command is routed depending on the client's ReadFrom strategy.
      java.util.concurrent.CompletableFuture<java.lang.Object> fcallReadOnly​(java.lang.String function)
      Invokes a previously loaded read-only function.
      This command is routed depending on the client's ReadFrom strategy.
      java.util.concurrent.CompletableFuture<java.lang.String> functionDelete​(GlideString libName)
      Deletes a library and all its functions.
      java.util.concurrent.CompletableFuture<java.lang.String> functionDelete​(java.lang.String libName)
      Deletes a library and all its functions.
      java.util.concurrent.CompletableFuture<byte[]> functionDump()
      Returns the serialized payload of all loaded libraries.
      java.util.concurrent.CompletableFuture<java.lang.String> functionFlush()
      Deletes all function libraries.
      java.util.concurrent.CompletableFuture<java.lang.String> functionFlush​(FlushMode mode)
      Deletes all function libraries.
      java.util.concurrent.CompletableFuture<java.lang.String> functionKill()
      Kills a function that is currently executing.
      FUNCTION KILL terminates read-only functions only.
      java.util.concurrent.CompletableFuture<java.util.Map<java.lang.String,​java.lang.Object>[]> functionList​(boolean withCode)
      Returns information about the functions and libraries.
      java.util.concurrent.CompletableFuture<java.util.Map<java.lang.String,​java.lang.Object>[]> functionList​(java.lang.String libNamePattern, boolean withCode)
      Returns information about the functions and libraries.
      java.util.concurrent.CompletableFuture<java.util.Map<GlideString,​java.lang.Object>[]> functionListBinary​(boolean withCode)
      Returns information about the functions and libraries.
      java.util.concurrent.CompletableFuture<java.util.Map<GlideString,​java.lang.Object>[]> functionListBinary​(GlideString libNamePattern, boolean withCode)
      Returns information about the functions and libraries.
      java.util.concurrent.CompletableFuture<GlideString> functionLoad​(GlideString libraryCode, boolean replace)
      Loads a library to Valkey.
      java.util.concurrent.CompletableFuture<java.lang.String> functionLoad​(java.lang.String libraryCode, boolean replace)
      Loads a library to Valkey.
      java.util.concurrent.CompletableFuture<java.lang.String> functionRestore​(byte[] payload)
      Restores libraries from the serialized payload returned by functionDump().
      java.util.concurrent.CompletableFuture<java.lang.String> functionRestore​(byte[] payload, FunctionRestorePolicy policy)
      Restores libraries from the serialized payload returned by functionDump()..
      java.util.concurrent.CompletableFuture<java.util.Map<java.lang.String,​java.util.Map<java.lang.String,​java.util.Map<java.lang.String,​java.lang.Object>>>> functionStats()
      Returns information about the function that's currently running and information about the available execution engines.
      FUNCTION STATS runs on all nodes of the server, including primary and replicas.
      java.util.concurrent.CompletableFuture<java.util.Map<java.lang.String,​java.util.Map<GlideString,​java.util.Map<GlideString,​java.lang.Object>>>> functionStatsBinary()
      Returns information about the function that's currently running and information about the available execution engines.
      FUNCTION STATS runs on all nodes of the server, including primary and replicas.
      java.util.concurrent.CompletableFuture<java.lang.Boolean[]> scriptExists​(GlideString[] sha1s)
      Checks existence of scripts in the script cache by their SHA1 digest.
      java.util.concurrent.CompletableFuture<java.lang.Boolean[]> scriptExists​(java.lang.String[] sha1s)
      Checks existence of scripts in the script cache by their SHA1 digest.
      java.util.concurrent.CompletableFuture<java.lang.String> scriptFlush()
      Flushes the Lua scripts cache.
      java.util.concurrent.CompletableFuture<java.lang.String> scriptFlush​(FlushMode flushMode)
      Flushes the Lua scripts cache.
      java.util.concurrent.CompletableFuture<java.lang.String> scriptKill()
      Kill the currently executing Lua script, assuming no write operation was yet performed by the script.
    • Method Detail

      • functionLoad

        java.util.concurrent.CompletableFuture<java.lang.String> functionLoad​(java.lang.String libraryCode,
                                                                              boolean replace)
        Loads a library to Valkey.
        Parameters:
        libraryCode - The source code that implements the library.
        replace - Whether the given library should overwrite a library with the same name if it already exists.
        Returns:
        The library name that was loaded.
        Since:
        Valkey 7.0 and above.
        See Also:
        valkey.io for details.
        Example:
        
         String code = "#!lua name=mylib \n redis.register_function('myfunc', function(keys, args) return args[1] end)";
         String response = client.functionLoad(code, true).get();
         assert response.equals("mylib");
         
      • functionLoad

        java.util.concurrent.CompletableFuture<GlideString> functionLoad​(GlideString libraryCode,
                                                                         boolean replace)
        Loads a library to Valkey.
        Parameters:
        libraryCode - The source code that implements the library.
        replace - Whether the given library should overwrite a library with the same name if it already exists.
        Returns:
        The library name that was loaded.
        Since:
        Valkey 7.0 and above.
        See Also:
        valkey.io for details.
        Example:
        
         GlideString code = gs("#!lua name=mylib \n redis.register_function('myfunc', function(keys, args) return args[1] end)");
         GlideString response = client.functionLoad(code, true).get();
         assert response.equals(gs("mylib"));
         
      • functionList

        java.util.concurrent.CompletableFuture<java.util.Map<java.lang.String,​java.lang.Object>[]> functionList​(boolean withCode)
        Returns information about the functions and libraries.
        Parameters:
        withCode - Specifies whether to request the library code from the server or not.
        Returns:
        Info about all libraries and their functions.
        Since:
        Valkey 7.0 and above.
        See Also:
        valkey.io for details.
        Example:
        
         Map<String, Object>[] response = client.functionList(true).get();
         for (Map<String, Object> libraryInfo : response) {
             System.out.printf("Server has library '%s' which runs on %s engine%n",
                 libraryInfo.get("library_name"), libraryInfo.get("engine"));
             Map<String, Object>[] functions = (Map<String, Object>[]) libraryInfo.get("functions");
             for (Map<String, Object> function : functions) {
                 Set<String> flags = (Set<String>) function.get("flags");
                 System.out.printf("Library has function '%s' with flags '%s' described as %s%n",
                     function.get("name"), String. join(", ", flags), function.get("description"));
             }
             System.out.printf("Library code:%n%s%n", libraryInfo.get("library_code"));
         }
         
      • functionListBinary

        java.util.concurrent.CompletableFuture<java.util.Map<GlideString,​java.lang.Object>[]> functionListBinary​(boolean withCode)
        Returns information about the functions and libraries.
        Parameters:
        withCode - Specifies whether to request the library code from the server or not.
        Returns:
        Info about all libraries and their functions.
        Since:
        Valkey 7.0 and above.
        See Also:
        valkey.io for details.
        Example:
        
         Map<String, Object>[] response = client.functionList(true).get();
         for (Map<String, Object> libraryInfo : response) {
             System.out.printf("Server has library '%s' which runs on %s engine%n",
                 libraryInfo.get("library_name"), libraryInfo.get("engine"));
             Map<String, Object>[] functions = (Map<String, Object>[]) libraryInfo.get("functions");
             for (Map<String, Object> function : functions) {
                 Set<String> flags = (Set<String>) function.get("flags");
                 System.out.printf("Library has function '%s' with flags '%s' described as %s%n",
                     function.get("name"), String. join(", ", flags), function.get("description"));
             }
             System.out.printf("Library code:%n%s%n", libraryInfo.get("library_code"));
         }
         
      • functionList

        java.util.concurrent.CompletableFuture<java.util.Map<java.lang.String,​java.lang.Object>[]> functionList​(java.lang.String libNamePattern,
                                                                                                                      boolean withCode)
        Returns information about the functions and libraries.
        Parameters:
        libNamePattern - A wildcard pattern for matching library names.
        withCode - Specifies whether to request the library code from the server or not.
        Returns:
        Info about queried libraries and their functions.
        Since:
        Valkey 7.0 and above.
        See Also:
        valkey.io for details.
        Example:
        
         Map<String, Object>[] response = client.functionList("myLib?_backup", true).get();
         for (Map<String, Object> libraryInfo : response) {
             System.out.printf("Server has library '%s' which runs on %s engine%n",
                 libraryInfo.get("library_name"), libraryInfo.get("engine"));
             Map<String, Object>[] functions = (Map<String, Object>[]) libraryInfo.get("functions");
             for (Map<String, Object> function : functions) {
                 Set<String> flags = (Set<String>) function.get("flags");
                 System.out.printf("Library has function '%s' with flags '%s' described as %s%n",
                     function.get("name"), String. join(", ", flags), function.get("description"));
             }
             System.out.printf("Library code:%n%s%n", libraryInfo.get("library_code"));
         }
         
      • functionListBinary

        java.util.concurrent.CompletableFuture<java.util.Map<GlideString,​java.lang.Object>[]> functionListBinary​(GlideString libNamePattern,
                                                                                                                       boolean withCode)
        Returns information about the functions and libraries.
        Parameters:
        libNamePattern - A wildcard pattern for matching library names.
        withCode - Specifies whether to request the library code from the server or not.
        Returns:
        Info about queried libraries and their functions.
        Since:
        Valkey 7.0 and above.
        See Also:
        valkey.io for details.
        Example:
        
         Map<String, Object>[] response = client.functionList("myLib?_backup", true).get();
         for (Map<String, Object> libraryInfo : response) {
             System.out.printf("Server has library '%s' which runs on %s engine%n",
                 libraryInfo.get("library_name"), libraryInfo.get("engine"));
             Map<String, Object>[] functions = (Map<String, Object>[]) libraryInfo.get("functions");
             for (Map<String, Object> function : functions) {
                 Set<String> flags = (Set<String>) function.get("flags");
                 System.out.printf("Library has function '%s' with flags '%s' described as %s%n",
                     function.get("name"), String. join(", ", flags), function.get("description"));
             }
             System.out.printf("Library code:%n%s%n", libraryInfo.get("library_code"));
         }
         
      • functionFlush

        java.util.concurrent.CompletableFuture<java.lang.String> functionFlush()
        Deletes all function libraries.
        Returns:
        OK.
        Since:
        Valkey 7.0 and above.
        See Also:
        valkey.io for details.
        Example:
        
         String response = client.functionFlush().get();
         assert response.equals("OK");
         
      • functionFlush

        java.util.concurrent.CompletableFuture<java.lang.String> functionFlush​(FlushMode mode)
        Deletes all function libraries.
        Parameters:
        mode - The flushing mode, could be either FlushMode.SYNC or FlushMode.ASYNC.
        Returns:
        OK.
        Since:
        Valkey 7.0 and above.
        See Also:
        valkey.io for details.
        Example:
        
         String response = client.functionFlush(SYNC).get();
         assert response.equals("OK");
         
      • functionDelete

        java.util.concurrent.CompletableFuture<java.lang.String> functionDelete​(java.lang.String libName)
        Deletes a library and all its functions.
        Parameters:
        libName - The library name to delete.
        Returns:
        OK.
        Since:
        Valkey 7.0 and above.
        See Also:
        valkey.io for details.
        Example:
        
         String response = client.functionDelete("myLib").get();
         assert response.equals("OK");
         
      • functionDelete

        java.util.concurrent.CompletableFuture<java.lang.String> functionDelete​(GlideString libName)
        Deletes a library and all its functions.
        Parameters:
        libName - The library name to delete.
        Returns:
        OK.
        Since:
        Valkey 7.0 and above.
        See Also:
        valkey.io for details.
        Example:
        
         String response = client.functionDelete(gs("myLib")).get();
         assert response.equals("OK");
         
      • functionDump

        java.util.concurrent.CompletableFuture<byte[]> functionDump()
        Returns the serialized payload of all loaded libraries.
        Returns:
        The serialized payload of all loaded libraries.
        Since:
        Valkey 7.0 and above.
        See Also:
        valkey.io for details.
        Example:
        
         byte[] data = client.functionDump().get();
         // now data could be saved to restore loaded functions on any Valkey instance
         
      • functionRestore

        java.util.concurrent.CompletableFuture<java.lang.String> functionRestore​(byte[] payload)
        Restores libraries from the serialized payload returned by functionDump().
        Parameters:
        payload - The serialized data from functionDump().
        Returns:
        OK.
        Since:
        Valkey 7.0 and above.
        See Also:
        valkey.io for details.
        Example:
        
         String response = client.functionRestore(data).get();
         assert response.equals("OK");
         
      • functionRestore

        java.util.concurrent.CompletableFuture<java.lang.String> functionRestore​(byte[] payload,
                                                                                 FunctionRestorePolicy policy)
        Restores libraries from the serialized payload returned by functionDump()..
        Parameters:
        payload - The serialized data from functionDump().
        policy - A policy for handling existing libraries.
        Returns:
        OK.
        Since:
        Valkey 7.0 and above.
        See Also:
        valkey.io for details.
        Example:
        
         String response = client.functionRestore(data, FLUSH).get();
         assert response.equals("OK");
         
      • fcall

        java.util.concurrent.CompletableFuture<java.lang.Object> fcall​(java.lang.String function)
        Invokes a previously loaded function.
        This command is routed to primary nodes only.
        To route to a replica please refer to fcallReadOnly(java.lang.String).
        Parameters:
        function - The function name.
        Returns:
        The invoked function's return value.
        Since:
        Valkey 7.0 and above.
        See Also:
        valkey.io for details.
        Example:
        
         Object response = client.fcall("Deep_Thought").get();
         assert response == 42L;
         
      • fcall

        java.util.concurrent.CompletableFuture<java.lang.Object> fcall​(GlideString function)
        Invokes a previously loaded function.
        This command is routed to primary nodes only.
        To route to a replica please refer to fcallReadOnly(java.lang.String).
        Parameters:
        function - The function name.
        Returns:
        The invoked function's return value.
        Since:
        Valkey 7.0 and above.
        See Also:
        valkey.io for details.
        Example:
        
         Object response = client.fcall(gs("Deep_Thought")).get();
         assert response == 42L;
         
      • fcallReadOnly

        java.util.concurrent.CompletableFuture<java.lang.Object> fcallReadOnly​(java.lang.String function)
        Invokes a previously loaded read-only function.
        This command is routed depending on the client's ReadFrom strategy.
        Parameters:
        function - The function name.
        Returns:
        The invoked function's return value.
        Since:
        Valkey 7.0 and above.
        See Also:
        valkey.io for details.
        Example:
        
         Object response = client.fcallReadOnly("Deep_Thought").get();
         assert response == 42L;
         
      • fcallReadOnly

        java.util.concurrent.CompletableFuture<java.lang.Object> fcallReadOnly​(GlideString function)
        Invokes a previously loaded read-only function.
        This command is routed depending on the client's ReadFrom strategy.
        Parameters:
        function - The function name.
        Returns:
        The invoked function's return value.
        Since:
        Valkey 7.0 and above.
        See Also:
        valkey.io for details.
        Example:
        
         Object response = client.fcallReadOnly(gs("Deep_Thought")).get();
         assert response == 42L;
         
      • functionKill

        java.util.concurrent.CompletableFuture<java.lang.String> functionKill()
        Kills a function that is currently executing.
        FUNCTION KILL terminates read-only functions only. FUNCTION KILL runs on all nodes of the server, including primary and replicas.
        Returns:
        OK if function is terminated. Otherwise, throws an error.
        Since:
        Valkey 7.0 and above.
        See Also:
        valkey.io for details.
        Example:
        
         String response = client.functionKill().get();
         assert response.equals("OK");
         
      • functionStats

        java.util.concurrent.CompletableFuture<java.util.Map<java.lang.String,​java.util.Map<java.lang.String,​java.util.Map<java.lang.String,​java.lang.Object>>>> functionStats()
        Returns information about the function that's currently running and information about the available execution engines.
        FUNCTION STATS runs on all nodes of the server, including primary and replicas. The response includes a mapping from node address to the command response for that node.
        Returns:
        A Map from node address to the command response for that node, where the command contains a Map with two keys:
        • running_script with information about the running script.
        • engines with information about available engines and their stats.
        See example for more details.
        Since:
        Valkey 7.0 and above.
        See Also:
        valkey.io for details.
        Example:
        
         Map<String, Map<String, Map<String, Object>>> response = client.functionStats().get();
         for (String node : response.keySet()) {
           Map<String, Object> runningScriptInfo = response.get(node).get("running_script");
           if (runningScriptInfo != null) {
             String[] commandLine = (String[]) runningScriptInfo.get("command");
             System.out.printf("Node '%s' is currently running function '%s' with command line '%s', which has been running for %d ms%n",
                 node, runningScriptInfo.get("name"), String.join(" ", commandLine), (long)runningScriptInfo.get("duration_ms"));
           }
           Map<String, Object> enginesInfo = response.get(node).get("engines");
           for (String engineName : enginesInfo.keySet()) {
             Map<String, Long> engine = (Map<String, Long>) enginesInfo.get(engineName);
             System.out.printf("Node '%s' supports engine '%s', which has %d libraries and %d functions in total%n",
                 node, engineName, engine.get("libraries_count"), engine.get("functions_count"));
           }
         }
         
      • functionStatsBinary

        java.util.concurrent.CompletableFuture<java.util.Map<java.lang.String,​java.util.Map<GlideString,​java.util.Map<GlideString,​java.lang.Object>>>> functionStatsBinary()
        Returns information about the function that's currently running and information about the available execution engines.
        FUNCTION STATS runs on all nodes of the server, including primary and replicas. The response includes a mapping from node address to the command response for that node.
        Returns:
        A Map from node address to the command response for that node, where the command contains a Map with two keys:
        • running_script with information about the running script.
        • engines with information about available engines and their stats.
        See example for more details.
        Since:
        Valkey 7.0 and above.
        See Also:
        valkey.io for details.
        Example:
        
         Map<GlideString, Map<GlideString, Map<GlideString, Object>>> response = client.functionStats().get();
         for (String node : response.keySet()) {
           Map<GlideString, Object> runningScriptInfo = response.get(gs(node)).get(gs("running_script"));
           if (runningScriptInfo != null) {
             GlideString[] commandLine = (GlideString[]) runningScriptInfo.get(gs("command"));
             System.out.printf("Node '%s' is currently running function '%s' with command line '%s', which has been running for %d ms%n",
                 node, runningScriptInfo.get(gs("name")), String.join(" ", Arrays.toString(commandLine)), (long)runningScriptInfo.get(gs("duration_ms")));
           }
           Map<GlideString, Object> enginesInfo = response.get(gs(node)).get(gs("engines"));
           for (GlideString engineName : enginesInfo.keySet()) {
             Map<GlideString, Long> engine = (Map<GlideString, Long>) enginesInfo.get(gs(engineName));
             System.out.printf("Node '%s' supports engine '%s', which has %d libraries and %d functions in total%n",
                 node, engineName, engine.get(gs("libraries_count")), engine.get(gs("functions_count")));
           }
         }
         
      • scriptExists

        java.util.concurrent.CompletableFuture<java.lang.Boolean[]> scriptExists​(java.lang.String[] sha1s)
        Checks existence of scripts in the script cache by their SHA1 digest.
        Parameters:
        sha1s - SHA1 digests of Lua scripts to be checked.
        Returns:
        An array of boolean values indicating the existence of each script.
        See Also:
        SCRIPT EXISTS for details.
        Example:
        
         try(Script luaScript = new Script("return { KEYS[1], ARGV[1] }", true)) {
             client.invokeScript(luaScript).get();
             Boolean[] result = client.scriptExists(new String[]{luaScript.getHash()});
             assert result[0].equals(true);
         }
         
      • scriptExists

        java.util.concurrent.CompletableFuture<java.lang.Boolean[]> scriptExists​(GlideString[] sha1s)
        Checks existence of scripts in the script cache by their SHA1 digest.
        Parameters:
        sha1s - SHA1 digests of Lua scripts to be checked.
        Returns:
        An array of boolean values indicating the existence of each script.
        See Also:
        SCRIPT EXISTS for details.
        Example:
        
         try(Script luaScript = new Script(gs("return { KEYS[1], ARGV[1] }", true))) {
             client.invokeScript(luaScript).get();
             Boolean[] result = client.scriptExists(new String[]{luaScript.getHash()});
             assert result[0].equals(true);
         }
         
      • scriptFlush

        java.util.concurrent.CompletableFuture<java.lang.String> scriptFlush()
        Flushes the Lua scripts cache.
        Returns:
        A simple "OK" response.
        See Also:
        SCRIPT FLUSH for details.
        Example:
        
         String result = client.scriptFlush();
         assert "OK".equals(result);
         
      • scriptFlush

        java.util.concurrent.CompletableFuture<java.lang.String> scriptFlush​(FlushMode flushMode)
        Flushes the Lua scripts cache.
        Parameters:
        flushMode - The flushing mode, could be either FlushMode.SYNC or FlushMode.ASYNC.
        Returns:
        A simple "OK" response.
        See Also:
        SCRIPT FLUSH for details.
        Example:
        
         String result = client.scriptFlush(ASYNC);
         assert "OK".equals(result);
         
      • scriptKill

        java.util.concurrent.CompletableFuture<java.lang.String> scriptKill()
        Kill the currently executing Lua script, assuming no write operation was yet performed by the script.
        Returns:
        A simple "OK" response.
        See Also:
        SCRIPT KILL for details.
        Example:
        
         String result = client.scriptKill();
         assert "OK".equals(result);