How does someone use Guava's CacheLoader asynchronously - caching

The question says it all I'd like to use CacheBuilder, but my values are pulled in asynchronously. This worked previously with MapMaker as the CacheLoader wasn't a requirement. Now I'd like to know if I can hack this up or if there are any non deprecated alternatives. Thank you.

I think the question you're trying to ask is "How can I use CacheBuilder without having to specify a CacheLoader?" If that's the case, then there will be support for this in Guava release 11.0. In the meantime a build() method on CacheLoader is already checked into trunk (as of this morning):
http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/cache/CacheBuilder.html

One method would be to make with generic parameters K and V as your desired outputs:
LoadingCache<K, ListenableFuture<V>> values = CacheBuilder.newBuilder()
.build(
new CacheLoader<K, ListenableFuture<V>>() {
public ListenableFuture<V> load(K key) {
/* Get your future */
}
});

Related

Get the method name of the callee in v8

Since nodejs >= 10 FunctionCallbackInfo::Callee has been deprecated (https://github.com/nodejs/nan/blob/master/CHANGELOG.md). I need to update a c++ code that uses v8, where the method name being called was used. How to get that now?
It is recommemded to use info.Data() instead. But I don't follow how to get the methods name from that. I guess it goes something like this:
void GetData(IN const Nan::FunctionCallbackInfo<v8::Value>& info)
{
v8::Local<v8::Function> data = v8::Local<v8::Function>::Cast(info.Data());
....
}
How do I get the methods name from data? From the documentation, looks like it cannot be done any longer (https://github.com/nodejs/nan/blob/master/doc/methods.md):
Note: FunctionCallbackInfo::Callee is removed in Node.js after 10.0.0 because it is was deprecated in V8. Consider using info.Data() to pass any information you need.
So, if no extra information is supplied, there is no way to get the name of the callee?
This did the trick:
v8::Local<v8::Function> out;
out = v8::Local<v8::Function>::Cast(info.Data());
v8::String::Utf8Value callee(out->GetName()->ToString());

Map Java Stream to Map of Objects with Object as Key

I have some classes and I am doing some work in a List<WorkDay> which contains a List<LedgerItem>, I have everything working but one part. Well it works, but not exactly how I would like it to.
public Stream<Map<WorkDay, Set<LedgerItem>>> adjustWorkDays(List<WorkDay> workDays) {
return workDays.stream()
.sorted((d1,d2) -> d1.getCreated().compareTo(d2.getCreated()))
.map(day -> createGroupByWorkDay(day))/*need it to collect here*/;
}
If you can see the return type is Stream<Map<WorkDay, Set<LedgerItem>>> but I want to map this out of the Stream as Map<WorkDay, Set<LedgerItem>> with a collector but just cannot seem to get Collectors.toMap() syntax to do anything but break.
Like I said, everything works perfectly, so I dont need anything outside of the mapping to work.
Just FYI: createGroupByWorkDay returns Map<WorkDay, Set<LedgerItem>> already but only accepts a single WorkDay as this is a requirement so I cannot change the way this is executed...
thanks in advance
EDIT:
So the method that I have createGroupByWorkDay that is not listed here works perfectly as expected, and will never be changed. It returns the correct type of Map<WorkDay, Set<LedgerItem>> but only has a signature for one WorkDay like this createGroupByWorkDay(WorkDay day) to the method in question in the original comment uses that to build individual Maps which are grouped by WorkDay and returns, but there could be N number of those, so the method public Stream<Map<WorkDay, Set<LedgerItem>>> adjustWorkDays(List<WorkDay> workDays) should return all of those Maps collected into one map in the collector. If that makes any sense?
Your question is not clear to me. But I guess you may be asking for something like this?
Map<WorkDay, List<LedgerItem>> result = workDays.stream()
.collect(Collectors.toMap(Function.identity(), WorkDay::getLedgerItems));
If not please explain your problem statement clearly. This is just a guess.
Here's an update,
Map<WorkDay, List<LedgerItem>> result = workDays.stream()
.map(d -> createGroupByWorkDay(d))
.flatMap(m -> m.entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

Adding a method to Bluebird promise

I have promisified Mongoose. I have some methods that extent Mongoose Query that now would need to be added to Bluebird. I don't mind extending Mongoose but do not want to take the same approach for this more gobal library. Looking through the docs I see some potentials but I am not sure.
I would like to come as close/clean as the following:
Model.findAsync().toCustom();
toCustom is basically a form of toJSON which 1) execs the query and 2) custom outputs the results/create custom errors etc... pretty straighforward.
What is the cleanest way to achieve something like the above? I would like to avoid doing this each time:
Model.findAsync().then(function(docs) {
return toCustom(docs);
}, function(err) {
return toCustom(err);
});
You get the idea...
Bluebird actually supports your use case directly. If you need to publish a library that extends bluebird in your own custom way you can get a fresh copy of bluebird by doing:
var Promise = require("bluebird/js/main/promise")();
Promise.promisifyAll(require("mongoose")); // promisify with a local copy
Promise.prototype.toCustom = function(){
return this.then(toCustom, toCustom); // assuming this isn't just `.finally`
};
You might also want to export it somehow. This feature is designed for library authors and for getting an isolated copy of bluebird. See the for library authors section in the wiki.

Using element "unless" for #Cacheable [spring 3.2.3] not working

I did use unless following for Cacheable, but it seems it did not work.
For the function below, it still will cache the result if result = -1.
Not sure what is the reason for its not working. Anyone gets any ideas? Any lib missing?
#Cacheable(value="queueIds", key="#servicePhoneNumber", unless="#result == -1")
public int getQueueIdByPhoneNumber(String servicePhoneNumber) { ... }
I had the same issue. The problem is that I used two different spring versions in a project.

Joomla 3.0 generic database error handling

Going from Joomla 2.5 to 3.0 with my extension, I'm struggling with how to do the DB error handling (since GetErrorNum is deprecated, see also Joomla! JDatabase::getErrorNum() is deprecated, use exception handling instead).
The way that seems to be the one to go according to the question linked above, is to add the following code for each db->query() code:
if (!$db->query()) {
throw new Exception($db->getErrorMsg());
}
In my opinion, that makes DB error handling more awkward than it was before. So far, I simply called a checkDBError() function after a DB call, which queried the ErrorNum and handled any possible error accordingly.
That was independent from how the DB query was actually triggered - there are different ways to do that, and different results on an error: $db->loadResult() returns null on error, $db->query() returns false. So there will now be different checks for different DB access types.
Isn't there any generic way to handle this, e.g. a way to tell Joomla to throw some exception on DB problems? Or do I have to write my own wrapper around the DatabaseDriver to achieve that? Or am I maybe missing something obvious?
Or should I just ignore the deprecation warning for now and continue with using getErrorNum()? I'd like to make my extension future-proof, but I also don't want to clutter it too much with awkward error handling logic.
Just found this discussion: https://groups.google.com/forum/#!msg/joomla-dev-general/O-Hp0L6UGcM/XuWLqu2vhzcJ
As I interpret it, there is that deprecation warning, but there is no proper replacement yet anyway...
Unless somebody points out any other proper documentation of how to do it in 3.0, I will keep to the getErrorNum method of doing stuff...
Get getErrorNum() function will solve your problem....
$result = $db->loadResult();
// Check for a database error.
if ($db->getErrorNum())
{
JFactory::getApplication()->enqueueMessage($db->getErrorMsg());
return false;
}

Resources