如何在游戏中执行以下操作:
我需要在游戏中创建一个全局可访问的对象,以便我可以从所有控制器访问它。
此对象将是用户对象的地图。
这可能吗?
答案 0 :(得分:1)
如果你想这样做,我想你可以使用DI和SimpleDateFormat的组合来建立原始缓存。
在你的一个guice模块中,你可以得到如下内容。
public static void attachFileviaEmail(Context p_context,String p_subject,String p_filePath)
{
Uri m_uri;
File m_file = new File(p_filePath);
String m_emailTo = "Receiver Email ID";
if(m_file.exists())
{
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{m_emailTo});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, p_subject);
m_uri = Uri.parse("file://" + p_filePath);
if(m_uri != null)
{
emailIntent.putExtra(Intent.EXTRA_STREAM, m_uri);
}
// emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Hi pl find attachment.....");
p_context.startActivity(Intent.createChooser(emailIntent, p_context.getString(R.string.lbl_send_email)));
}
}
然后,在您的类中,无论您需要使用缓存,请注入该地图:
import scala.collection.concurrent
@Provides @Named("localUserCache") @Singleton
private def provideLocalUserCache(): concurrent.Map[Long, User] = {
concurrent.TrieMap[Long, User]()
}
import scala.collection.concurrent
@Singleton
class Application @Inject() (@Named ("localUserCache") val cache: concurrent.Map[Long, User], ... ) { ... }
仅用于一些额外的清晰度,如果您只有一个并发映射的实例,则可以删除它。
然后,您可以执行以下操作:
@Named
此处,只有在地图中不存在相应的键时才会执行cache.getOrElseUpdate(someId, getUserFromDb(someId))
。来自文档:
如果指定的键不在地图中,则使用给定的thunk op计算其值并将其输入到地图中。
由于并发映射不能为键或值包含null,因此如果thunk op返回null,则抛出NullPointerException。
此外,为了使事情更简洁,您可以使用封装地图的注入实例替换注入的地图。对于前。
getUserFromDb(someId)
答案 1 :(得分:0)
您可以将其作为单身人士。
控制器中的某个地方
@Inject
private Users users;
你的实际课程:
public class MyUsers implements Users ....
Guice模块类:
public class MyModule extends AbstractModule {
@Override
protected void configure() {
bind(Users.class).to(MyUsers.class).asEagerSingleton();
}
}
application.conf 中的
play.modules.enabled += "mypackage.MyModule"
一些文档:https://www.playframework.com/documentation/2.4.x/JavaDependencyInjection#Eager-bindings
scala也一样: https://www.playframework.com/documentation/2.4.x/ScalaDependencyInjection#Eager-bindings
https://www.playframework.com/documentation/2.4.x/ScalaDependencyInjection