Answered by:
Cachmanager how to d0 couchbase json bucket configuration

Question
-
User-2103114970 posted
Hi ,
could you please help me ,
I am using cache manager from (http://cachemanager.michaco.net) how to do json configuration for Couchbase , We are trying to connect couchbase buckets using dotnet core and json but it failed to read bucket details.
{
// "$schema": "http://cachemanager.michaco...",
"couchbase": {
"useSsl": "false",
"operationLifespan": "12000",
"servers": [ "http://Localhost:8091"" ],
"buckets": [
{
"name": "test",
"useSsl": "false",
"password": "test123",
"connectionPool": {
"name": "custom",
"maxSize": "10",
"minSize": "5"
}
}
]
},
"cacheManagers": [
{
"maxRetries": 1000,
"name": "cachename",
"retryTimeout": 100,
"updateMode": "None",
"handles": [
{
"key": "couchbase",
"knownType": "Couchbase",
"type": ""
}
],
"serializer": { "knownType": "Json" }
}
]
Please help me as soon as possible, we are struggling since 10 days.
Thanks
Murali
Thursday, December 21, 2017 7:52 PM
Answers
-
User1168443798 posted
Hi mkonanki,
It is much complex to convert such project .net 462 to .net core 2.0. If your requirement is converting such a project, you may need to submit an issue on https://github.com/MichaCo/CacheManager/issues to ask for .net core 2.0 version.
If you have any trouble to read configuration from appsettings, please share us a simple project with the settings, then, we could check the root cause.
Best Regards,
Edward
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, December 26, 2017 5:06 AM -
User-2103114970 posted
Hi All,
I got solution,
Please find code below
- Under ServiceCollectionExtensions class add property public static IConfiguration ConfigurationRoot { get; set; }
- in method AddCacheManagerConfiguration Assign ConfigurationRoot = fromConfiguration;
- Create new class at couchbase configuration project
public static class CouchBaseConnection
{
private const string CouchBaseClientKeyName = "couchbase";
public static CouchbaseClientDefinition GetCouchbaseClientDefinition()
{
var couchbaseClientDefinition = new CouchbaseClientDefinition();
ServiceCollectionExtensions.ConfigurationRoot.GetSection(CouchBaseClientKeyName).Bind(couchbaseClientDefinition);
return couchbaseClientDefinition;
}
}
4. Add into bucket cachhandle for coucbase
var couchBaseClientDefinition = GetCouchbaseClientDefinition();
if (couchBaseClientDefinition != null)
{
additionalSettings.BucketName = couchBaseClientDefinition?.Buckets?[0].Name;
additionalSettings.BucketPassword = couchBaseClientDefinition?.Buckets?[0].Password;
}
5. in CouchbaseConfigurationManager class add cluster details
public static ClientConfiguration GetConfiguration(string configurationKeyOrSectionName)
{
NotNullOrWhiteSpace(configurationKeyOrSectionName, nameof(configurationKeyOrSectionName));
if (_configurations.TryGetValue(configurationKeyOrSectionName, out ClientConfiguration configuration))
{
return configuration;
}
ClientConfiguration clientConfiguration = new ClientConfiguration();
var couchBaseClientDefinition = GetCouchbaseClientDefinition();
if (couchBaseClientDefinition != null)
clientConfiguration = new ClientConfiguration(couchBaseClientDefinition);
ClusterHelper.Initialize(clientConfiguration);
_configurations.TryAdd(configurationKeyOrSectionName, clientConfiguration);
return clientConfiguration;
}
private static ICluster GetCluster(string configurationKey)
{
NotNullOrWhiteSpace(configurationKey, nameof(configurationKey));
if (!_clusters.TryGetValue(configurationKey, out ICluster cluster))
{
var config = GetConfiguration(configurationKey);
if (config != null)
{
cluster = new Cluster(config);
_clusters.TryAdd(configurationKey, cluster);
}
7. Configuration
{
"couchbase": {
"operationLifespan":"12000",
"servers": [
"http://TestServerName:8091",
"http://TestServerName:8091",
"http://TestServerName:8091",
"http://TestServerName:8091"
],
"buckets": [
{
"name": "BucketName",
"password": "BucketPassword"
}
]
},
"cacheManagers": [
{
"maxRetries": 1000,
"name": "cachename",
"retryTimeout": 100,
"updateMode": "None",
//"loggerFactory": {
// "knownType": "Microsoft"
//},
"serializer": {
"knownType": "Json"
},
"handles": [
{
"knownType": "Couchbase",
"type": "",
"key": "BucketName",
"isBackplaneSource": true,
"Name": "Couchbase"
}
]
}
]
}
if you can insert value but not able to get value then implement ISerilization
[Serializable]
public class CacheItem<T> : ICacheItemProperties, ISerializable
protected CacheItem(SerializationInfo info, StreamingContext context)
{
NotNull(info, nameof(info));
Key = info.GetString(nameof(Key));
Value = (T)info.GetValue(nameof(Value), typeof(T));
ValueType = (Type)info.GetValue(nameof(ValueType), typeof(Type));
Region = info.GetString(nameof(Region));
ExpirationMode = (ExpirationMode)info.GetValue(nameof(ExpirationMode), typeof(ExpirationMode));
ExpirationTimeout = (TimeSpan)info.GetValue(nameof(ExpirationTimeout), typeof(TimeSpan));
CreatedUtc = info.GetDateTime(nameof(CreatedUtc));
LastAccessedUtc = info.GetDateTime(nameof(LastAccessedUtc));
UsesExpirationDefaults = info.GetBoolean(nameof(UsesExpirationDefaults));
}
/// <summary>
/// Populates a <see cref="T:System.Runtime.Serialization.SerializationInfo"/> with the data
/// needed to serialize the target object.
/// </summary>
/// <param name="info">
/// The <see cref="T:System.Runtime.Serialization.SerializationInfo"/> to populate with data.
/// </param>
/// <param name="context">
/// The destination (see <see cref="T:System.Runtime.Serialization.StreamingContext"/>) for
/// this serialization.
/// </param>
/// <exception cref="System.ArgumentNullException">If info is null.</exception>
public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
{
NotNull(info, nameof(info));
info.AddValue(nameof(Key), Key);
info.AddValue(nameof(Value), Value);
info.AddValue(nameof(ValueType), ValueType);
info.AddValue(nameof(Region), Region);
info.AddValue(nameof(ExpirationMode), ExpirationMode);
info.AddValue(nameof(ExpirationTimeout), ExpirationTimeout);
info.AddValue(nameof(CreatedUtc), CreatedUtc);
info.AddValue(nameof(LastAccessedUtc), LastAccessedUtc);
info.AddValue(nameof(UsesExpirationDefaults), UsesExpirationDefaults);
}
Thanks,
Murali.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, December 27, 2017 6:46 PM
All replies
-
User1168443798 posted
Hi mkonanki,<o:p></o:p>
>> We are trying to connect couchbase buckets using dotnet core and json but it failed to read bucket detail<o:p></o:p>
Do you mean you want to read “buckets” values in appsettings.json?<o:p></o:p>
If so, you could implement a custom Options which corresponding to “buckets”, and map them like below:<o:p></o:p>
services.Configure<MySubOptions>(Configuration.GetSection("subsection"));
You could refer links below for more information.<o:p></o:p>
# Suboptions configuration<o:p></o:p>
Best Regards,<o:p></o:p>
Edward<o:p></o:p>
Friday, December 22, 2017 6:06 AM -
User-2103114970 posted
Hi Edward,
I am trying to do that, but when i call couchbase handle event , it is expecting BucketCacheHandleAdditionalConfiguration , how to pass custom class values to this handle .
public BucketCacheHandle(ICacheManagerConfiguration managerConfiguration, CacheHandleConfiguration configuration, ILoggerFactory loggerFactory, BucketCacheHandleAdditionalConfiguration additionalSettings)
: base(managerConfiguration, configuration)
{
Could you please help me .
Appreciate your help.
Thanks,
Murali
Friday, December 22, 2017 3:33 PM -
User1168443798 posted
Hi mkonanki,<o:p></o:p>
>> I am trying to do that, but when i call couchbase handle event , it is expecting BucketCacheHandleAdditionalConfiguration , how to pass custom class values to this handle<o:p></o:p>
What is BucketCacheHandleAdditionalConfiguration? <o:p></o:p>
For construct inject, you need to register BucketCacheHandleAdditionalConfiguration in startup.cs.<o:p></o:p>
Is it a class map to buckets in appsettings.json?<o:p></o:p>
If so, you need to register it like below:<o:p></o:p>
services.Configure<BucketCacheHandleAdditionalConfiguration>(Configuration.GetSection("buckets"));
If you still fail to make it work, you may consider sharing us a simple project which could reproduce your issue.<o:p></o:p>
Best Regards,<o:p></o:p>
Edward<o:p></o:p>
Monday, December 25, 2017 8:13 AM -
User-2103114970 posted
Hi Edward,
Thanks for your help,
I mapped in startup.cs but still it is not working.
What is BucketCacheHandleAdditionalConfiguration? This is cachmenager provided predefined calss http://cachemanager.michaco.net/documentation/CacheManagerConfiguration
"BucketDetails": {
"BucketName": "Test",
"BucketPassword": "Test123"
},
services.Configure<BucketCacheHandleAdditionalConfiguration>(op => Configuration.GetSection("BucketDetails").Bind(op));// This values binding but not coming to bucketcachhndle.
public BucketCacheHandle(ICacheManagerConfiguration managerConfiguration, CacheHandleConfiguration configuration, ILoggerFactory loggerFactory, BucketCacheHandleAdditionalConfiguration additionalSettings)
: base(managerConfiguration, configuration)
{
additionalSettings values not coming.
Thanks,
Murali.
Monday, December 25, 2017 9:46 PM -
User1168443798 posted
Hi mkonanki,
Could you share us a simple project which could reproduce your issue?
Best Regards,
Edward
Tuesday, December 26, 2017 2:30 AM -
User-2103114970 posted
Hi Edward,
Please down load source code from below link.
http://cachemanager.michaco.net/
Thanks,
Murali
Tuesday, December 26, 2017 2:40 AM -
User1168443798 posted
Hi mkonanki,
I have downloaded the project and build correctly at my side. But I assume this is not your project, is this a library you want to use?
Please share us how to reproduce your issue with this project.
Best Regards,
Edward
Tuesday, December 26, 2017 2:58 AM -
User-2103114970 posted
Hi Edward,
I am using same project converting to .net core 2.0 webapi with json configuration, Redis json configuration already provided but couch base json configuration not available .
I need couchbase json configuration with same project.
Please let me know if you need any information.
Appreciate your help.
Thanks,
Murali.
Tuesday, December 26, 2017 4:55 AM -
User1168443798 posted
Hi mkonanki,
It is much complex to convert such project .net 462 to .net core 2.0. If your requirement is converting such a project, you may need to submit an issue on https://github.com/MichaCo/CacheManager/issues to ask for .net core 2.0 version.
If you have any trouble to read configuration from appsettings, please share us a simple project with the settings, then, we could check the root cause.
Best Regards,
Edward
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, December 26, 2017 5:06 AM -
User-2103114970 posted
Thank you so much Edward.
Tuesday, December 26, 2017 5:08 PM -
User-2103114970 posted
Hi All,
I got solution,
Please find code below
- Under ServiceCollectionExtensions class add property public static IConfiguration ConfigurationRoot { get; set; }
- in method AddCacheManagerConfiguration Assign ConfigurationRoot = fromConfiguration;
- Create new class at couchbase configuration project
public static class CouchBaseConnection
{
private const string CouchBaseClientKeyName = "couchbase";
public static CouchbaseClientDefinition GetCouchbaseClientDefinition()
{
var couchbaseClientDefinition = new CouchbaseClientDefinition();
ServiceCollectionExtensions.ConfigurationRoot.GetSection(CouchBaseClientKeyName).Bind(couchbaseClientDefinition);
return couchbaseClientDefinition;
}
}
4. Add into bucket cachhandle for coucbase
var couchBaseClientDefinition = GetCouchbaseClientDefinition();
if (couchBaseClientDefinition != null)
{
additionalSettings.BucketName = couchBaseClientDefinition?.Buckets?[0].Name;
additionalSettings.BucketPassword = couchBaseClientDefinition?.Buckets?[0].Password;
}
5. in CouchbaseConfigurationManager class add cluster details
public static ClientConfiguration GetConfiguration(string configurationKeyOrSectionName)
{
NotNullOrWhiteSpace(configurationKeyOrSectionName, nameof(configurationKeyOrSectionName));
if (_configurations.TryGetValue(configurationKeyOrSectionName, out ClientConfiguration configuration))
{
return configuration;
}
ClientConfiguration clientConfiguration = new ClientConfiguration();
var couchBaseClientDefinition = GetCouchbaseClientDefinition();
if (couchBaseClientDefinition != null)
clientConfiguration = new ClientConfiguration(couchBaseClientDefinition);
ClusterHelper.Initialize(clientConfiguration);
_configurations.TryAdd(configurationKeyOrSectionName, clientConfiguration);
return clientConfiguration;
}
private static ICluster GetCluster(string configurationKey)
{
NotNullOrWhiteSpace(configurationKey, nameof(configurationKey));
if (!_clusters.TryGetValue(configurationKey, out ICluster cluster))
{
var config = GetConfiguration(configurationKey);
if (config != null)
{
cluster = new Cluster(config);
_clusters.TryAdd(configurationKey, cluster);
}
7. Configuration
{
"couchbase": {
"operationLifespan":"12000",
"servers": [
"http://TestServerName:8091",
"http://TestServerName:8091",
"http://TestServerName:8091",
"http://TestServerName:8091"
],
"buckets": [
{
"name": "BucketName",
"password": "BucketPassword"
}
]
},
"cacheManagers": [
{
"maxRetries": 1000,
"name": "cachename",
"retryTimeout": 100,
"updateMode": "None",
//"loggerFactory": {
// "knownType": "Microsoft"
//},
"serializer": {
"knownType": "Json"
},
"handles": [
{
"knownType": "Couchbase",
"type": "",
"key": "BucketName",
"isBackplaneSource": true,
"Name": "Couchbase"
}
]
}
]
}
if you can insert value but not able to get value then implement ISerilization
[Serializable]
public class CacheItem<T> : ICacheItemProperties, ISerializable
protected CacheItem(SerializationInfo info, StreamingContext context)
{
NotNull(info, nameof(info));
Key = info.GetString(nameof(Key));
Value = (T)info.GetValue(nameof(Value), typeof(T));
ValueType = (Type)info.GetValue(nameof(ValueType), typeof(Type));
Region = info.GetString(nameof(Region));
ExpirationMode = (ExpirationMode)info.GetValue(nameof(ExpirationMode), typeof(ExpirationMode));
ExpirationTimeout = (TimeSpan)info.GetValue(nameof(ExpirationTimeout), typeof(TimeSpan));
CreatedUtc = info.GetDateTime(nameof(CreatedUtc));
LastAccessedUtc = info.GetDateTime(nameof(LastAccessedUtc));
UsesExpirationDefaults = info.GetBoolean(nameof(UsesExpirationDefaults));
}
/// <summary>
/// Populates a <see cref="T:System.Runtime.Serialization.SerializationInfo"/> with the data
/// needed to serialize the target object.
/// </summary>
/// <param name="info">
/// The <see cref="T:System.Runtime.Serialization.SerializationInfo"/> to populate with data.
/// </param>
/// <param name="context">
/// The destination (see <see cref="T:System.Runtime.Serialization.StreamingContext"/>) for
/// this serialization.
/// </param>
/// <exception cref="System.ArgumentNullException">If info is null.</exception>
public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
{
NotNull(info, nameof(info));
info.AddValue(nameof(Key), Key);
info.AddValue(nameof(Value), Value);
info.AddValue(nameof(ValueType), ValueType);
info.AddValue(nameof(Region), Region);
info.AddValue(nameof(ExpirationMode), ExpirationMode);
info.AddValue(nameof(ExpirationTimeout), ExpirationTimeout);
info.AddValue(nameof(CreatedUtc), CreatedUtc);
info.AddValue(nameof(LastAccessedUtc), LastAccessedUtc);
info.AddValue(nameof(UsesExpirationDefaults), UsesExpirationDefaults);
}
Thanks,
Murali.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, December 27, 2017 6:46 PM