Skip to content

Commit

Permalink
Translate code snippets to VB.NET
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimir-litvinchik committed May 11, 2024
1 parent 3c3319b commit 2eaa766
Show file tree
Hide file tree
Showing 83 changed files with 5,659 additions and 190 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
deploy_key
common/
bin/
obj/
obj/
.vs
61 changes: 60 additions & 1 deletion net/developer-guide/caching-results/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@ The following code snippet shows how to enable caching and displays the differen
{{< tabs "example1">}}
{{< tab "C#" >}}
```csharp
// Specify parameters.
using System;
using System.Diagnostics;
using System.IO;
using GroupDocs.Viewer;
using GroupDocs.Viewer.Caching;
using GroupDocs.Viewer.Options;
// ...
string outputDirectory = @"C:\output";
string cachePath = Path.Combine(outputDirectory, "cache");
string pageFilePathFormat = Path.Combine(outputDirectory, "page_{0}.html");
Expand All @@ -48,6 +55,58 @@ using (Viewer viewer = new Viewer(@"C:\sample.docx", settings))
}
```
{{< /tab >}}
{{< tab "VB.NET">}}
```vb
Imports System
Imports System.Diagnostics
Imports System.IO
Imports GroupDocs.Viewer
Imports GroupDocs.Viewer.Caching
Imports GroupDocs.Viewer.Options
' ...

Module Program
Sub Main(args As String())
Dim outputDirectory As String = "C:\output"
Dim cachePath As String = Path.Combine(outputDirectory, "cache")
Dim pageFilePathFormat As String = Path.Combine(outputDirectory, "page_{0}.html")
' Create a cache.
Dim cache As FileCache = New FileCache(cachePath)
Dim settings As ViewerSettings = New ViewerSettings(cache)

Using viewer As Viewer = New Viewer("C:\sample.docx", settings)
' Create an HTML file.
Dim options As HtmlViewOptions = HtmlViewOptions.ForEmbeddedResources(pageFilePathFormat)
' Render and display the rendering time.
Dim stopWatch As Stopwatch = Stopwatch.StartNew()
viewer.View(options)
stopWatch.[Stop]()
Console.WriteLine("Time taken on first call to View method {0} (ms).", stopWatch.ElapsedMilliseconds)
' Get cached results and display the time.
stopWatch.Restart()
viewer.View(options)
stopWatch.[Stop]()
Console.WriteLine("Time taken on second call to View method {0} (ms).", stopWatch.ElapsedMilliseconds)
End Using
End Sub
End Module
```
{{< /tab >}}
{{< tab "VB.NET">}}
```vb
CONVERSION ERROR: Conversion for GlobalStatement not implemented, please report this issue in 'string outputDirectory = @"...' at character 160

CONVERSION ERROR: Conversion for GlobalStatement not implemented, please report this issue in 'string cachePath = Path.Com...' at character 200

CONVERSION ERROR: Conversion for GlobalStatement not implemented, please report this issue in 'string pageFilePathFormat =...' at character 260

CONVERSION ERROR: Conversion for GlobalStatement not implemented, please report this issue in 'FileCache cache = new FileC...' at character 357

CONVERSION ERROR: Conversion for GlobalStatement not implemented, please report this issue in 'ViewerSettings settings = n...' at character 402

CONVERSION ERROR: Conversion for GlobalStatement not implemented, please report this issue in 'using (Viewer viewer = new ...' at character 458
```
{{< /tab >}}
{{< /tabs >}}

The following image shows a sample console output:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,19 @@ The following code snippet shows how to implement a custom caching using Redis C
{{< tabs "example1">}}
{{< tab "C#" >}}
```csharp
using GroupDocs.Viewer;
using System;
using System.IO;
using System.Linq;
using System.Diagnostics;
using System.Collections.Generic;
using System.Xml.Serialization;

using GroupDocs.Viewer.Caching;
using GroupDocs.Viewer.Options;
using GroupDocs.Viewer;

using StackExchange.Redis;
// ...
// Specify the cache parameters.
var serverAddress = "127.0.0.1:6379";
Expand Down Expand Up @@ -65,7 +74,6 @@ static void PrintTimeTaken(Action action, string format)
Console.WriteLine(format, stopwatch.ElapsedMilliseconds);
}

// Realize cache.
public class RedisCache : ICache, IDisposable
{
private readonly string _cacheKeyPrefix;
Expand Down Expand Up @@ -176,6 +184,148 @@ public class RedisCache : ICache, IDisposable
}
```
{{< /tab >}}
{{< tab "VB.NET">}}
```vb
Imports System
Imports System.IO
Imports System.Linq
Imports System.Diagnostics
Imports System.Collections.Generic
Imports System.Runtime.InteropServices
Imports System.Xml.Serialization

Imports GroupDocs.Viewer.Caching
Imports GroupDocs.Viewer.Options
Imports GroupDocs.Viewer

Imports StackExchange.Redis

Module Program
Public Sub Main()
' Specify the cache parameters.
Dim serverAddress As String = "127.0.0.1:6379"
Dim filePath As String = "sample.docx"

' Create the cache.
Dim cache As RedisCache = New RedisCache(serverAddress, filePath)
Dim settings As ViewerSettings = New ViewerSettings(cache)

Using viewer As Viewer = New Viewer(filePath, settings)
' Create HTML files.
Dim viewOptions As HtmlViewOptions = HtmlViewOptions.ForEmbeddedResources()

' Display rendering time.
PrintTimeTaken(Sub() viewer.View(viewOptions), "The first call to View method took {0} ms.")
' Display time to get cached results.
PrintTimeTaken(Sub() viewer.View(viewOptions), "The second call to View method took {0} ms.")
End Using
End Sub

' Get and display time taken.
Private Sub PrintTimeTaken(action As Action, format As String)
Dim stopwatch As Stopwatch = New Stopwatch()
stopwatch.Start()
action.Invoke()
stopwatch.Stop()

Console.WriteLine(format, stopwatch.ElapsedMilliseconds)
End Sub


Public Class RedisCache
Implements ICache, IDisposable

Private ReadOnly _cacheKeyPrefix As String
Private ReadOnly _redis As ConnectionMultiplexer
Private ReadOnly _db As IDatabase
Private ReadOnly _host As String

Public Sub New(host As String, cacheKeyPrefix As String)
_host = host
_cacheKeyPrefix = cacheKeyPrefix
_redis = ConnectionMultiplexer.Connect(_host)
_db = _redis.GetDatabase()
End Sub

Private Function CopyToMemoryStream(data As Stream) As MemoryStream
Dim result As New MemoryStream()
data.Position = 0
data.CopyTo(result)
Return result
End Function

Public Sub [Set](key As String, value As Object) Implements ICache.[Set]
If value Is Nothing Then
Return
End If

Dim prefixedKey As String = GetPrefixedKey(key)
Dim memoryStream As MemoryStream = Nothing

If TypeOf value Is Stream Then
memoryStream = If(TypeOf value Is MemoryStream, DirectCast(value, MemoryStream), CopyToMemoryStream(DirectCast(value, Stream)))
Else
memoryStream = New MemoryStream()
Dim serializer As New XmlSerializer(value.GetType())
serializer.Serialize(memoryStream, value)
End If

_db.StringSet(prefixedKey, RedisValue.CreateFrom(memoryStream))
End Sub

Public Function TryGetValue(Of TEntry)(key As String, <Out> ByRef value As TEntry) As Boolean Implements ICache.TryGetValue
Dim prefixedKey As String = GetPrefixedKey(key)
Dim redisValue As RedisValue = _db.StringGet(prefixedKey)
If redisValue.HasValue Then
Dim data As Object = If(GetType(TEntry) = GetType(Stream), ReadStream(redisValue), Deserialize(Of TEntry)(redisValue))
value = DirectCast(data, TEntry)
Return True
End If

value = Nothing
Return False
End Function

Public Function ICache_GetKeys(filter As String) As IEnumerable(Of String) Implements ICache.GetKeys
Return _redis.GetServer(_host).Keys(pattern:=$"*{filter}*").Select(Function(x) x.ToString().Replace(_cacheKeyPrefix, String.Empty)).Where(Function(x) x.StartsWith(filter, StringComparison.InvariantCultureIgnoreCase)).ToList()
End Function

Private Function GetPrefixedKey(key As String) As String
Return $"{_cacheKeyPrefix}{key}"
End Function

Private Function ReadStream(redisValue As RedisValue) As Object
Return New MemoryStream(redisValue)
End Function

Private Function Deserialize(Of T)(redisValue As RedisValue) As T
Dim data As Object
Using stream As New MemoryStream(redisValue)
Dim serializer As New XmlSerializer(GetType(T))

Try
data = serializer.Deserialize(stream)
Catch ex As InvalidOperationException
data = Nothing
Catch ex As NullReferenceException
data = Nothing
End Try
End Using

Return DirectCast(data, T)
End Function

Public Sub Dispose()
_redis.Dispose()
End Sub

Public Sub IDisposable_Dispose() Implements IDisposable.Dispose
Throw New NotImplementedException
End Sub
End Class
End Module
```
{{< /tab >}}
{{< /tabs >}}

The following image shows a sample console output:
Expand Down
Loading

0 comments on commit 2eaa766

Please sign in to comment.