Table of Contents |
---|
Caching per path
...
SInce Slice 4.1 you can cache your SliceResources created for a given path within single request processing.This is especially helpful if you have a heavy object (which reads a lot of properties) and you are using it a couple of times within single request, e.g. PageModel which keeps your page properties - this model may be often referenced by other components and there's no need to re-create it each time. Please note that caching can only be applied for resources which are not changed while a request is processed, otherwise such changes won't be reflected in cached objects.
Additionally, caching works not only within request context but also in osgi-services context. It's basically based on Slice's @ContextScope
.
To make your SliceResources cacheable per path you should simply annotate them with @Cacheable
annotation.
Code Block |
---|
@SliceResource
@Cacheable
public class PageModel {
@JcrProperty("jcr:title")
private String title;
...
} |
Simpler OSGi service support
If you want to use OSGi services within your guicified code, you can simple go for Peaberry solution which allows you to bind an interface to OSGi service (Slice is shipped with Peaberry out of the box). Normally, you bind OSGi services in some Guice module, e.g.
Code Block |
---|
public class MyAppModule extends AbstractModule {
@Override
public void configure() {
bind(ResourceResolverFactory.class).toProvider(Peaberry.service(ResourceResolverFactory.class)).single());
// other bindings
}
}
public class MyClass {
@Inject
ResourceResolverFactory factory;
...
} |
From now on, you can simplify this declarations and Slice will do the work for you. You just need to annotate field or constructor argument with @OsgiService
and Slice will bind such an interface/class to a Peaberry provider, making it injectable. So your code can just look like this:
Code Block |
---|
public class MyClass {
@Inject
@OsgiService
ResourceResolverFactory factory;
...
} |
Injector registration
Injector listener
...